Opened 8 years ago

Closed 8 years ago

Last modified 5 years ago

#10413 closed (fixed)

RelatedManager.add fails silently when adding an object of the wrong type

Reported by: David Gouldin Owned by: nobody
Component: Database layer (models, ORM) Version: 1.0
Severity: Keywords:
Cc: dgouldin@… Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

When trying to add an object of the wrong type using a related manager, Django fails silently rather than complaining as specified in the docs:

http://docs.djangoproject.com/en/dev/topics/db/queries/#saving-foreignkey-and-manytomanyfield-fields

Example:

class ParentModel(models.Model):
    name = models.CharField(max_length=200)

class ChildModel(models.Model):
    name = models.CharField(max_length=200)
    parent = models.ForeignKey(ParentModel, related_name="child_models")

class NotAChildModel(models.Model):
    name = models.CharField(max_length=200)

p = ParentModel(name='Parent')
p.save()
nc = NotAChildModel(name='Not A Child')
nc.save()
p.child_models.add(nc) # This should blow up, but doesn't
p.child_models.all() # Returns []

Attachments (2)

10413.diff (3.3 KB) - added by David Gouldin 8 years ago.
10413_2.diff (3.1 KB) - added by David Gouldin 8 years ago.

Download all attachments as: .zip

Change History (9)

Changed 8 years ago by David Gouldin

Attachment: 10413.diff added

comment:1 Changed 8 years ago by David Gouldin

Has patch: set

comment:2 Changed 8 years ago by Jacob

milestone: 1.1
Triage Stage: UnreviewedAccepted

comment:3 Changed 8 years ago by Russell Keith-Magee

Component: UncategorizedDatabase layer (models, ORM)

Changed 8 years ago by David Gouldin

Attachment: 10413_2.diff added

comment:4 Changed 8 years ago by David Gouldin

Cc: dgouldin@… added
Triage Stage: AcceptedReady for checkin

Updated patch to move import of Model class into ManyRelatedManager._add_items.

comment:5 Changed 8 years ago by Jacob

Resolution: fixed
Status: newclosed

(In [10226]) Fixed #10413: RelatedManager.add no longer fails silenty when trying to add an object of the wrong type. Thanks, dgouldin.

comment:6 Changed 8 years ago by Jacob

(In [10293]) [1.0.X] Fixed #10413: RelatedManager.add no longer fails silenty when trying to add an object of the wrong type. Thanks, dgouldin.

Backport of r10226 from trunk.

comment:7 Changed 5 years ago by Jacob

milestone: 1.1

Milestone 1.1 deleted

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