Opened 15 years ago

Closed 15 years ago

Last modified 13 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: no UI/UX: no

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 15 years ago.
10413_2.diff (3.1 KB ) - added by David Gouldin 15 years ago.

Download all attachments as: .zip

Change History (9)

by David Gouldin, 15 years ago

Attachment: 10413.diff added

comment:1 by David Gouldin, 15 years ago

Has patch: set

comment:2 by Jacob, 15 years ago

milestone: 1.1
Triage Stage: UnreviewedAccepted

comment:3 by Russell Keith-Magee, 15 years ago

Component: UncategorizedDatabase layer (models, ORM)

by David Gouldin, 15 years ago

Attachment: 10413_2.diff added

comment:4 by David Gouldin, 15 years ago

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

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

comment:5 by Jacob, 15 years ago

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 by Jacob, 15 years ago

(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 by Jacob, 13 years ago

milestone: 1.1

Milestone 1.1 deleted

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