﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
8387	Returning a subset of the models fields in ModelAdmin.get_fieldsets only works when also overriding get_form	 	nobody	"If you return only a subset of your models fields in ``get_fieldsets`` you need to override ``get_form`` as well and set the ``fields`` or ``exclude`` attribute. Otherwise the form will expect *all* fields to be present and will either raise validation errors on the missing fields or clear them if they have ``blank=True``.

This won't work:

{{{
#!python
class MyModelAdmin(admin.ModelAdmin):
    def get_fieldsets(self, request, obj=None):
        if obj:
            return [(None, {'fields': ('field_c', 'field_b')})]
        return [(None, {'fields': ('field_a', 'field_b', 'field_c')})]
}}}

But this will:

{{{
#!python
class MyModelAdmin(admin.ModelAdmin):
    def get_fieldsets(self, request, obj=None):
        if obj:
            return [(None, {'fields': ('field_c', 'field_b')})]
        return [(None, {'fields': ('field_a', 'field_b', 'field_c')})]

    def get_form(self, request, obj=None, **kwargs):
        if obj:
            defaults = {'exclude': ('field_a',)}
        else:
            defaults = {}
        defaults.update(kwargs)
        return super(MyModelAdmin, self).get_form(request, obj, **defaults)
}}}

The reason for this is that Django cannot provide a sensible default for both cases:

 1. Just override ``get_form``
 2. Just override ``get_fieldsets``

In the first case ``get_fieldsets`` calls the overriden ``get_form`` and can so determine a default value for fieldsets. But this means that the default implementation of ``get_form`` cannot call ``get_fieldsets`` as this would create an infinite loop.

Which function calls the other could be discussed -- maybe overriding ``get_fieldsets`` is much more common? But in the case it stays like it is the attached patch documents the behavior."	Bug	closed	Documentation	dev	Normal	fixed			Accepted	1	0	0	0	0	0
