Ticket #8387: get_form+get_fieldsets-docs-01.diff

File get_form+get_fieldsets-docs-01.diff, 2.6 KB (added by , 16 years ago)

Updated the patch for the docs refactor

  • docs/ref/contrib/admin.txt

     
    587587                instance.save()
    588588            formset.save_m2m()
    589589
     590``get_form(self, request, obj=None, **kwargs)``
     591~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     592
     593The ``get_form`` method is given the ``HttpRequest`` and a model
     594instance, but only if the user is changing the object. It can also be passed
     595keyword arguments to be used as attributes for the ``ModelForm.Meta``
     596class. The return value is a ``ModelForm``.
     597
     598For example to dynamically add another field to the form when the user has extra rights::
     599
     600    class MyModelAdminForm(forms.ModelForm):
     601        class Meta:
     602            model = MyModel
     603        extra_option = forms.BooleanField()
     604   
     605    class MyModelAdmin(admin.ModelAdmin):
     606        def get_form(self, request, obj=None, **kwargs):
     607            if request.user.has_perm("myapp.can_use_extra_option"):
     608                return MyModelAdminForm
     609            return super(MyModelAdmin, self).get_form(request, obj, **kwargs)
     610
     611``get_fieldsets(self, request, obj=None)``
     612~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     613
     614The ``get_fieldsets`` method is given the ``HttpRequest`` and a model
     615instance, but only if the user is changing the object. It returns the fieldsets
     616in the same format they are specified in the ``fieldsets`` attribute.
     617
     618For example to display a smaller set of fields when the user is changing
     619rather than adding::
     620
     621    class MyModelAdmin(admin.ModelAdmin):
     622        def get_fieldsets(self, request, obj=None):
     623            if obj:
     624                return [(None, {'fields': ('field_c', 'field_b')})]
     625            return [(None, {'fields': ('field_a', 'field_b', 'field_c')})]
     626   
     627        def get_form(self, request, obj=None, **kwargs):
     628            if obj:
     629                defaults = {'exclude': ('field_a',)}
     630            else:
     631                defaults = {}
     632            defaults.update(kwargs)
     633            return super(MyModelAdmin, self).get_form(request, obj, **defaults)
     634
     635.. admonition:: Note
     636
     637    If you return only a subset of your models fields in ``get_fieldsets`` you need
     638    to override ``get_form`` as well and set the ``fields`` or ``exclude`` attribute.
     639    Otherwise the form will expect *all* fields to be present and will either raise
     640    validation errors on the missing fields or clear them if they have ``blank=True``.
     641
    590642``ModelAdmin`` media definitions
    591643--------------------------------
    592644
Back to Top