﻿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
35696	ModelAdmin is overruling the fields of ModelForm	Jurrian Tromp		"It seems that in ModelForm both `field_order` and `Meta.fields` ordering is being overruled by the admin change view. When rendering, the `form` object initially has the correct order, but when `admin_form` (django.contrib.admin.options:1826) is created, the `fieldsets` (django.contrib.admin.options:1781) coming from `get_fieldsets()` overrides the correct order in `form.fields`.

Something similar in #19514 has been reported before, more or less saying that ModelAdmin order should be leading. The problem with this however is that the reason you have a ModelForm here this way is because you try to do something more specialized. Consider this:

{{{
class Message(models.Model):
    some_readonly = models.CharField(...)
    overruled_model_m2m_field = models.ManyToManyField(through=SomeModel, ...)

class MessageForm(forms.ModelForm):
    some_form_field = forms.CharField()
    overruled_model_m2m_field = forms.CharField()

    field_order = ['some_form_field', 'overruled_model_m2m_field']

    class Meta:
        model = Message
        fields = ['some_form_field', 'overruled_model_m2m_field']
        exclude = ['some_readonly']

class MessageAdmin(admin.ModelAdmin):
    readonly_fields = ['some_readonly']
    form = MessageForm
}}}

**Resulting in the following unexpected behavior:**
1. For the fields MessageForm is ignored for the most part, MessageAdmin.get_fields() is de-facto shown.
 a. ModelAdmin `readonly_fields` are shown, even when they are excluded in the ModelForm.
 b. `MessageForm.field_order` has no effect when used in an admin view.
2. When a model field has `through`, it is not shown, as it should be an inline. Although it will be added to the fields again in the form (`MessageForm.overruled_model_m2m_field`)
 a. This makes it appear ""randomly"" in the ordering as it is updated to the end of base_fields (django.forms.models:324)

**Expected behavior:**
1. `MessageForm` takes precedence over `MessageAdmin`. 
  a. Excluded fields are not shown
  b. `ModelForm.Meta.fields` and `ModelForm.field_order` are used in favor of `ModelAdmin.get_fields()`
2. a. Fields with a through that are also specified in the form itself are ordered correctly according to 1b.
"	Bug	new	Forms	4.2	Normal				Unreviewed	0	0	0	0	0	0
