Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 7th, 2012  |  syntax: None  |  size: 0.90 KB  |  hits: 10  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How to make the ContentType foreignkey i18n?
  2. from django.utils.translation import ugettext_lazy as _
  3.  
  4. class Event(models.Model):
  5.     ...
  6.  
  7.     class Meta:
  8.         verbose_name = _(u'Event')
  9.         verbose_name_plural = _(u'Events')
  10.        
  11. def content_type_choices(**kwargs):
  12.     content_types = []
  13.     for content_type in ContentType.objects.filter(**kwargs):
  14.         content_types.append((content_type.pk, content_type.model_class()._meta.verbose_name))
  15.  
  16.     return content_types
  17.  
  18. LIMIT_CHOICES_TO = {'model__startswith': 'pageapp_'}
  19.  
  20. class PageWAForm(forms.ModelForm):
  21.     app_page_type = forms.ModelChoiceField(queryset=ContentType.objects.filter(**LIMIT_CHOICES_TO),
  22.                                            empty_label=None)
  23.  
  24.     def __init__(self, *args, **kwargs):
  25.         super(PageWAForm, self).__init__(*args, **kwargs)
  26.         self.fields['app_page_type'].choices = content_type_choices(**LIMIT_CHOICES_TO)