Opened 7 years ago

Closed 6 years ago

Last modified 4 years ago

#10413 closed (fixed)

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

Reported by: dgouldin 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 dgouldin 7 years ago.
10413_2.diff (3.1 KB) - added by dgouldin 6 years ago.

Download all attachments as: .zip

Change History (9)

Changed 7 years ago by dgouldin

comment:1 Changed 7 years ago by dgouldin

  • Has patch set
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

comment:2 Changed 7 years ago by jacob

  • milestone set to 1.1
  • Triage Stage changed from Unreviewed to Accepted

comment:3 Changed 6 years ago by russellm

  • Component changed from Uncategorized to Database layer (models, ORM)

Changed 6 years ago by dgouldin

comment:4 Changed 6 years ago by dgouldin

  • Cc dgouldin@… added
  • Triage Stage changed from Accepted to Ready for checkin

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

comment:5 Changed 6 years ago by jacob

  • Resolution set to fixed
  • Status changed from new to closed

(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 6 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 4 years ago by jacob

  • milestone 1.1 deleted

Milestone 1.1 deleted

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