Code

Opened 4 years ago

Closed 4 years ago

#12351 closed (invalid)

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

Reported by: fredbartle 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: UI/UX:

Description

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 fredbartle 4 years ago.
Override get() and get_or_create() behavior to GenericRelatedObjectManager

Download all attachments as: .zip

Change History (2)

Changed 4 years ago by fredbartle

Override get() and get_or_create() behavior to GenericRelatedObjectManager

comment:1 Changed 4 years ago by fredbartle

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to invalid
  • Status changed from new to closed

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

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.