Conflict between ModelForm.Meta.exclude and ModelAdmin.readonly attributes
|Reported by:||msgre_valise||Owned by:||nobody|
|Severity:||Normal||Keywords:||ModelForm ModelAdmin readonly_fields exclude conflict|
|Cc:||Triage Stage:||Ready for checkin|
|Has patch:||yes||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
I have problem with contrib.admin application. Consider we have demo application with this three files:
from django.db import models class Demo(models.Model): name = models.CharField(max_length=100) note = models.TextField(blank=True, null=True)
from django.contrib import admin from models import Demo from forms import DemoAdminForm class DemoAdmin(admin.ModelAdmin): readonly_fields = ('name', ) form = DemoAdminForm admin.site.register(Demo, DemoAdmin)
from django import forms from models import Demo class DemoAdminForm(forms.ModelForm): class Meta: model = Demo exclude = ('note', )
If I go to administration, and edit some Demo object, I see two fields: editable Note and readonly Name. But! I should see only readonly Name (because I use own ModelForm class which excluded note field). Try to comment line "readonly_fields = ('name', )" at admin.py file. You will see, that in this situation (no readonly attribute on ModelAdmin) Django correctly accept Meta class on ModelForm and exclude field Note.
I think that there is a problem in the way, how Django construct ModelForm class. It don't respect Meta classes in Form, if there is at same time readonly attribute on ModelAdmin.
Change History (11)
comment:1 Changed 6 years ago by
|Patch needs improvement:||unset|
|Triage Stage:||Unreviewed → Accepted|