#19733 closed Cleanup/optimization (fixed)
Deprecate ModelForm without explicit fields
| Reported by: | Aymeric Augustin | Owned by: | Luke Plant |
|---|---|---|---|
| Component: | Forms | Version: | dev |
| Severity: | Normal | Keywords: | |
| Cc: | tom@… | Triage Stage: | Accepted |
| Has patch: | no | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Change History (8)
comment:1 by , 13 years ago
| Cc: | added |
|---|
comment:2 by , 13 years ago
| Triage Stage: | Unreviewed → Accepted |
|---|
comment:3 by , 13 years ago
| Owner: | changed from to |
|---|---|
| Status: | new → assigned |
comment:4 by , 13 years ago
| Summary: | Deprecate exclude in ModelForm → Deprecate ModelForm without explicit fields |
|---|
comment:5 by , 13 years ago
comment:6 by , 13 years ago
Another issue I'm hitting is to do with ModelAdmin validation.
If you are defining a custom ModelForm to be used with ModelAdmin, then you don't want to add fields = "__all__" just to allow it to be created, since the ModelAdmin should specify that as a default (and does). But if you define Meta.model, but not Meta.fields, you will get an error with the proposed changes.
There is a way around this problem for the common case - simply don't define the Meta class at all, or don't define Meta.model - you don't need it, because ModelAdmin can create it for you or set the model attribute if it is missing. However, if you do this, you hit the problem described here:
comment:7 by , 13 years ago
| Resolution: | → fixed |
|---|---|
| Status: | assigned → closed |
I have made some progress on this:
https://github.com/spookylukey/django/tree/ticket_19733
In case I don't get to work on it in a while, here are some comments, for myself as much as for anyone else.
This patch has turned out to be a bit tricky, for the following reasons:
ModelForm- as well as subclassingModelFormdirectly, there is alsomodelform_factoryandmodelformset_factoryetc. For users to receive nice clear deprecation warnings, all these functions have to be altered, which is a bit ugly.ModelFormmetaclass, which will (eventually) stop even the creation of aModelFormsubclass that has definedMeta.modelbut not one ofMeta.fieldsorMeta.exclude.ModelForm, modelform_factory etc., including allowing customModelForms.ModelFormmetaclass, but we want that to (eventually) be an error, but only if the form created is not for use in the admin, which is of course impossible to know.This behaviour goes both ways - if a custom
ModelFormdefines fields, and theModelAdmindoes not, theModelForm's fields should be used. If theModelAdmindefines fields (in various ways), andModelFormdoes not, then that list should be used. If both define fields, theModelAdminoverrides.With the branch as it stands, if you are creating a
ModelFormwhich is to be used for the admin, note:1) If you define the inner Meta and set 'model', you have to set 'fields' too (or 'exclude'), but its value will be ignored if you set the fields in any way in the
ModelAdmin.2) If you don't define the inner
Meta, you can avoid this, and the innerMetaclass will be set up for you automatically and correctly.3) But you must define the inner
Metaif you explicitly define fields in the custom form e.g.my_extra_field = IntegerField...ormy_overridden_field = TextField...etc.(otherwise you get validation errors)