from django import forms
from django.conf import settings

from mptt.forms import MPTTAdminForm

from .models import Page


TEMPLATE_CHOICES = getattr(settings, 'PAGE_TEMPLATES', (
    ('', 'Default'),
))


class PageAdminForm(MPTTAdminForm):
    class Meta:
        model = Page
        exclude = ()

    def __init__(self, *args, **kwargs):
        super(PageAdminForm, self).__init__(*args, **kwargs)

        # The list of templates is defined in settings, however as we can't have dynamic choices in
        # models due to migrations - we change the form choices instead.
        self.fields['template_name'] = forms.ChoiceField(choices=TEMPLATE_CHOICES, required=False)

from django import forms
from django.conf import settings

from mptt.forms import MPTTAdminForm

from .models import Page


TEMPLATE_CHOICES = getattr(settings, 'PAGE_TEMPLATES', (
    ('', 'Default'),
))


class PageAdminForm(MPTTAdminForm):
    class Meta:
        model = Page
        exclude = ()
        widgets = {
            # The list of templates is defined in settings, however as we can't have dynamic
            # choices in models due to migrations - we change the form choices instead.
            'template_name': forms.widgets.Select(choices=TEMPLATE_CHOICES),
        }

Use custom widget for template choices instead
Use custom widget for template choices instead

A bit more Djangonic than tweaking self.fields

  from django import forms
  from django.conf import settings
  
  from mptt.forms import MPTTAdminForm
  
  from .models import Page
  
  
  TEMPLATE_CHOICES = getattr(settings, 'PAGE_TEMPLATES', (
      ('', 'Default'),
  ))
  
  
  class PageAdminForm(MPTTAdminForm):
      class Meta:
          model = Page
          exclude = ()
+         widgets = {
+             # The list of templates is defined in settings, however as we can't have dynamic
+             # choices in models due to migrations - we change the form choices instead.
+             'template_name': forms.widgets.Select(choices=TEMPLATE_CHOICES),
+         }
  
-     def __init__(self, *args, **kwargs):
-         super(PageAdminForm, self).__init__(*args, **kwargs)
- 
-         # The list of templates is defined in settings, however as we can't have dynamic choices in
-         # models due to migrations - we change the form choices instead.
-         self.fields['template_name'] = forms.ChoiceField(choices=TEMPLATE_CHOICES, required=False)
- 
Use custom widget for template choices instead
## Code Before:
from django import forms
from django.conf import settings

from mptt.forms import MPTTAdminForm

from .models import Page


TEMPLATE_CHOICES = getattr(settings, 'PAGE_TEMPLATES', (
    ('', 'Default'),
))


class PageAdminForm(MPTTAdminForm):
    class Meta:
        model = Page
        exclude = ()

    def __init__(self, *args, **kwargs):
        super(PageAdminForm, self).__init__(*args, **kwargs)

        # The list of templates is defined in settings, however as we can't have dynamic choices in
        # models due to migrations - we change the form choices instead.
        self.fields['template_name'] = forms.ChoiceField(choices=TEMPLATE_CHOICES, required=False)

## Instruction:
Use custom widget for template choices instead
## Code After:
from django import forms
from django.conf import settings

from mptt.forms import MPTTAdminForm

from .models import Page


TEMPLATE_CHOICES = getattr(settings, 'PAGE_TEMPLATES', (
    ('', 'Default'),
))


class PageAdminForm(MPTTAdminForm):
    class Meta:
        model = Page
        exclude = ()
        widgets = {
            # The list of templates is defined in settings, however as we can't have dynamic
            # choices in models due to migrations - we change the form choices instead.
            'template_name': forms.widgets.Select(choices=TEMPLATE_CHOICES),
        }
