Opened 6 years ago

Last modified 6 years ago

#30286 closed New feature

Model's ID is being overwritten when GenericForeignKey is assigned — at Version 2

Reported by: Samuel Labrador Owned by: nobody
Component: contrib.contenttypes Version: 2.1
Severity: Normal Keywords: GenericForeignKey Content
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 Samuel Labrador)

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 = DailyLogHistory(
	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

Change History (2)

comment:1 by Samuel Labrador, 6 years ago

Description: modified (diff)

comment:2 by Samuel Labrador, 6 years ago

Description: modified (diff)
Note: See TracTickets for help on using tickets.
Back to Top