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 the
`super().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! :)
--
Ticket URL: <https://code.djangoproject.com/ticket/31102>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* status: new => closed
* resolution: => needsinfo
Comment:
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.)
--
Ticket URL: <https://code.djangoproject.com/ticket/31102#comment:1>