[Django] #30286: Model's ID is being overwritten when GenericForeignKey is assigned

2 views
Skip to first unread message

Django

unread,
Mar 24, 2019, 5:16:39 PM3/24/19
to django-...@googlegroups.com
#30286: Model's ID is being overwritten when GenericForeignKey is assigned
-------------------------------------+-------------------------------------
Reporter: Samuel | Owner: nobody
Labrador |
Type: Bug | Status: new
Component: | Version: 2.1
contrib.contenttypes | Keywords: GenericForeignKey
Severity: Normal | Content
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
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

}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/30286>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Mar 24, 2019, 5:17:40 PM3/24/19
to django-...@googlegroups.com
#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:

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')

}}}

{{{
incident = DailyLog.objects.get(id=1)

}}}

--

--
Ticket URL: <https://code.djangoproject.com/ticket/30286#comment:1>

Django

unread,
Mar 24, 2019, 5:18:56 PM3/24/19
to django-...@googlegroups.com
#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(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
>
> }}}

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

}}}

--

--
Ticket URL: <https://code.djangoproject.com/ticket/30286#comment:2>

Django

unread,
Mar 24, 2019, 9:24:21 PM3/24/19
to django-...@googlegroups.com
#30286: Model's ID is being overwritten when GenericForeignKey is assigned
-------------------------------------+-------------------------------------
Reporter: Samuel Labrador | Owner: nobody
Type: New feature | Status: closed
Component: | Version: 2.1
contrib.contenttypes |
Severity: Normal | Resolution: invalid

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
-------------------------------------+-------------------------------------
Changes (by Samuel Labrador):

* status: new => closed
* resolution: => invalid
* type: Bug => New feature


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(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
>
> }}}

New description:

Not a bug. Stupid error on my part. The GenericForeignKey id is the same
as that specified in the model.

--

--
Ticket URL: <https://code.djangoproject.com/ticket/30286#comment:3>

Reply all
Reply to author
Forward
0 new messages