#30286: Model's ID is being overwritten when GenericForeignKey is assigned -------------------------------------+------------------------------------- Reporter: Samuel Labrador | Owner: nobody Type: Bug | Status: new Component: | Version: 2.1 contrib.contenttypes | Severity: Normal | Resolution: Keywords: GenericForeignKey | Triage Stage: Content | Unreviewed Has patch: 0 | Needs documentation: 0 Needs tests: 0 | Patch needs improvement: 0 Easy pickings: 0 | UI/UX: 0 -------------------------------------+------------------------------------- Description changed by Samuel Labrador:
Old description: > When a Model with a GenericForeignKey field is assigned, the model's id > is overwritten with the GenericForeignKey's id on assignment. > > I am using a mySQL database (Ver 14.14 Distrib 5.6.28), Python3(3.7.2), > and django(2.1.7) > > Models: > {{{ > class Incident(models.Model): > start_timestamp = models.DateTimeField(auto_now=True) > class Meta: > abstract = True > > class DailyLog(Incident): > date = models.DateField(auto_now=True) > entries = GenericRelation(DailyLogData, object_id_field='id', > content_type_field='incident_type') > > class Data(models.Model): > creator = models.CharField(max_length=255) > creator_details = models.TextField(blank=True, null=True, > default=None) > timestamp = models.DateTimeField(auto_now=True) > incident_type = models.ForeignKey(ContentType, > on_delete=models.CASCADE, primary_key=False, unique=False, blank=True, > null=True) > incident_object = GenericForeignKey('incident_type', 'id') > class Meta: > abstract = True > > class DailyLogHistory(Data): > selections = CSVField(blank=True, null=True, default=None) > > }}} > > code that produces the bug: > > {{{ > incident = DailyLog.objects.get(id=1) > > x = DailyLogData( > selections=['123456'], > creator='sam', > ) > x.save() > > print(x) # Shows the correct auto-incremented id != 1 > x.incident_object = incident > print(x) # Shows that id == 1 > > }}} New description: When a Model with a GenericForeignKey field is assigned, the model's id is overwritten with the GenericForeignKey's id on assignment. I am using a mySQL database (Ver 14.14 Distrib 5.6.28), Python3(3.7.2), and django(2.1.7) Models: {{{ class Incident(models.Model): start_timestamp = models.DateTimeField(auto_now=True) class Meta: abstract = True class DailyLog(Incident): date = models.DateField(auto_now=True) entries = GenericRelation(DailyLogHistory, object_id_field='id', content_type_field='incident_type') class Data(models.Model): creator = models.CharField(max_length=255) creator_details = models.TextField(blank=True, null=True, default=None) timestamp = models.DateTimeField(auto_now=True) incident_type = models.ForeignKey(ContentType, on_delete=models.CASCADE, primary_key=False, unique=False, blank=True, null=True) incident_object = GenericForeignKey('incident_type', 'id') class Meta: abstract = True class DailyLogHistory(Data): selections = CSVField(blank=True, null=True, default=None) }}} code that produces the bug: {{{ incident = DailyLog.objects.get(id=1) x = DailyLogData( selections=['123456'], creator='sam', ) x.save() print(x) # Shows the correct auto-incremented id != 1 x.incident_object = incident print(x) # Shows that id == 1 }}} -- -- Ticket URL: <https://code.djangoproject.com/ticket/30286#comment:1> Django <https://code.djangoproject.com/> The Web framework for perfectionists with deadlines. -- You received this message because you are subscribed to the Google Groups "Django updates" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-updates+unsubscr...@googlegroups.com. To post to this group, send email to django-updates@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-updates/072.83247844b6acc81abcd2be9d98f5c833%40djangoproject.com. For more options, visit https://groups.google.com/d/optout.