Opened 17 years ago
Closed 17 years ago
#7280 closed (invalid)
newforms-admin fields marked "editable=False" raise error when used in "prepoluated_fields"
| Reported by: | peschler | Owned by: | nobody |
|---|---|---|---|
| Component: | contrib.admin | Version: | newforms-admin |
| Severity: | Keywords: | newforms-admin editable prepopulated_fields | |
| Cc: | Triage Stage: | Unreviewed | |
| Has patch: | no | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
Consider the following model and the associated ModelAdmin (shortened for demonstration):
class Menu(models.Model):
name = models.CharField(max_length=50, unique=True)
slug = models.SlugField(editable=False)
# ...
class MenuOptions(admin.ModelAdmin):
prepopulated_fields = {'slug': ('name',)}
display_list = ('name', 'slug',)
admin.site.register(Menu, MenuOptions)
The slug field should not be editable by a user but rather is updated in the save() method of the model everytime the name of the menu changes.
Now when I try to open the (newforms) admin page for adding a new Menu instance I get the following error:
KeyError at /admin/newnavigation/menu/add/ "Key 'slug' not found in Form"
The error disappears as soon I exchange "editable=False" with "editable=True" or remove the slug from the prepoluated_fields.
Seems there is a problem when using a field marked editable=False in the ModelAdmin's prepopulated_fields.
In my use case I can simply remove slug from the prepopulated_fields because I populate it manually, but there might be some cases where such a simple fix is not possible.
This is the expected behavior.
prepopulated_fieldsrequires that the fields you specify in both the key and value tuple to be present in the form.prepopulated_fieldswill not magically write functionality in the your model'ssavemethod. It provides some Javascript to deal with it on the user interface also allowing the user to override the automatically generated value. Please take this to django-users mailing list and/or #django on freenode.