Opened 9 years ago

Closed 9 years ago

#12351 closed (invalid)

GenericRelatedObjectManager get() and get_or_create() should behave like create()

Reported by: Fred Bartle Owned by: nobody
Component: Contrib apps Version: 1.1
Severity: Keywords: generic relation
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


Given the following models and objects:

class Attribute(models.Model):
    content_type = models.ForeignKey(ContentType)
    object_id = models.PositiveIntegerField()
    name = models.CharField(max_length=100)

class MyModel(models.Model):
    name = models.CharField(max_length=100)
    attributes = generic.GenericRelation(Attribute)

>>> obj = MyModel.objects.create(name="fred")

I can create Attributes for my objects easily, without specifying content_type and object_id. The create() interface adds the content_type and object_id to kwargs.

>>> obj.attributes.create(name="myattr")

When using get() and get_or_create() without specifying content_type and object_id:

>>> obj.attributes.get_or_create(name="myattr")
IntegrityError: null value in column "content_type_id" violates not-null constraint.

Obviously, in order to use get() and get_or_create(), I need to specify content_type and object_id. I propose to use the same behavior as create() for ease of use.

Attachments (1)

t12351-r11807.diff (1.2 KB) - added by Fred Bartle 9 years ago.
Override get() and get_or_create() behavior to GenericRelatedObjectManager

Download all attachments as: .zip

Change History (2)

Changed 9 years ago by Fred Bartle

Attachment: t12351-r11807.diff added

Override get() and get_or_create() behavior to GenericRelatedObjectManager

comment:1 Changed 9 years ago by Fred Bartle

Resolution: invalid
Status: newclosed

Nevermind, this appears to be working just fine as get_query_set() in GenericRelatedObjectManager does exactly what it should. I'm going crazy.

Note: See TracTickets for help on using tickets.
Back to Top