Opened 15 years ago
Closed 15 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 |
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)
Change History (2)
by , 15 years ago
Attachment: | t12351-r11807.diff added |
---|
comment:1 by , 15 years ago
Resolution: | → invalid |
---|---|
Status: | new → closed |
Nevermind, this appears to be working just fine as get_query_set() in GenericRelatedObjectManager does exactly what it should. I'm going crazy.
Override get() and get_or_create() behavior to GenericRelatedObjectManager