Opened 7 years ago
Last modified 7 years ago
#29649 closed Bug
admin readonly_fields with unique_together raises IntegrityError — at Version 1
| Reported by: | Harm Verhagen | Owned by: | nobody |
|---|---|---|---|
| Component: | Forms | Version: | 2.0 |
| Severity: | Normal | Keywords: | unique_together IntegrityError |
| Cc: | Triage Stage: | Unreviewed | |
| Has patch: | no | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description (last modified by )
I'm getting an IntegrityError when editing a model in the admin, when one of the fields of a unique_together pair is in readonly_fields
example
#models.py
class Foo(models.Model):
a = models.IntegerField()
b = models.IntegerField()
class Meta:
unique_together = (('a', 'b'), )
#admin.py
class FooAdmin(admin.ModelAdmin):
list_display = ('id', 'a', 'b')
readonly_fields = ('b', )
steps to reproduce
In django shell create two instances
>>> from data.models import Foo >>> Foo.objects.create(a=1,b=1) <Foo: Foo object (1)> >>> Foo.objects.create(a=2,b=1) <Foo: Foo object (2)>
Then in the admin edit Foo object 1 and change a to 2. (introducing a duplicate)
expected result
A form validation error in the admin: Foo with this A and B already exists.
actual result
A server 500 error (IntegrityError)
IntegrityError at /admin/data/foo/3/change/ duplicate key value violates unique constraint "data_foo_a_b_ac92595b_uniq" DETAIL: Key (a, b)=(1, 1) already exists.
notes
When removing the readonly_fields from the admin, all works as expected.
Maybe related to #13091 (similar issue when list_editable is used instead of readonly_fields)