Opened 5 years ago
Closed 5 years ago
#31102 closed Bug (needsinfo)
Bug: Saving on a ModelAdmin with TabularInlines sometimes duplicates rows
Reported by: | alex zhang | Owned by: | nobody |
---|---|---|---|
Component: | Uncategorized | Version: | 1.11 |
Severity: | Normal | Keywords: | admin, duplicate, tabularinline |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
Hi! Wondering if anyone has seen this before and if this is a known bug? Sadly (and I get how this makes this bug kind of difficult to interpret for you all, sorry about that!), I can't reproduce it consistently.
If this is inappropriate for a ticket since it's not consistently reproducible, apologies for the noise and I would super appreciate if you reached out to me individually if you have any ideas!
Background
We have the following models:
class Survey(models.Model): organization = models.OneToOneField('organization.Organization', on_delete=models.CASCADE, null=True, blank=True) class SurveyQuestion(models.Model): survey = models.ForeignKey('survey.Survey', on_delete=models.CASCADE) text = models.CharField(max_length=255) section_name = models.CharField(max_length=255)
They are being used for the following admins:
class SurveyQuestionInline(admin.TabularInline): model = SurveyQuestion fields = ('text', 'section_name',) extra = 0 class SurveyAdmin(admin.ModelAdmin): inlines = (SurveyQuestionInline,) raw_id_fields = ('organization',) fields = ('organization',) def save_related(self, request, form, formsets, change): organization_id = form.instance.organization_id # do something with organization_id that's irrelevant to these models super().save_related(request, form, formsets, change)
Problem
Sometimes when saving the page for SurveyAdmin, the SurveyQuestion's will duplicate.
Details
These are some of my findings after trying to look into this...
- After adding some logging in
SurveyAdmin.save_related
, it looks like SurveyQuestion's don't get duplicated until after thesuper().save_related(...)
- Once duplication happens once, duplication will continue to happen on subsequent saves of the admin page
- If changes to
SurveyQuestion.section_name
happen on SurveyAdmin, the changes will only be applied on the duplicated rows, not on the original SurveyQuestion
Thanks for taking the time to read this! :)
Sorry, there's really just not enough to go on here.
You must be doing something somewhere that's clearing the IDs from the submitted row, so leading to inserts, rather than updates, but there's no way of seeing that from what you've provided.
If you can narrow it down to a minimal project, then we can have a look. (But in so doing I suspect you'll find your bug.)