Ticket #13085: 13085.diff
File 13085.diff, 3.4 KB (added by , 15 years ago) |
---|
-
django/contrib/contenttypes/generic.py
49 49 # Convenience function using get_model avoids a circular import when 50 50 # using this model 51 51 ContentType = get_model("contenttypes", "contenttype") 52 if obj :52 if obj is not None: 53 53 return ContentType.objects.db_manager(obj._state.db).get_for_model(obj) 54 elif id :54 elif id is not None: 55 55 return ContentType.objects.db_manager(using).get_for_id(id) 56 56 else: 57 57 # This should never happen. I love comments like this, don't you? … … 72 72 # performance when dealing with GFKs in loops and such. 73 73 f = self.model._meta.get_field(self.ct_field) 74 74 ct_id = getattr(instance, f.get_attname(), None) 75 if ct_id :75 if ct_id is not None: 76 76 ct = self.get_content_type(id=ct_id, using=instance._state.db) 77 77 try: 78 78 rel_obj = ct.get_object_for_this_type(pk=getattr(instance, self.fk_field)) -
tests/regressiontests/generic_relations_regress/tests.py
70 70 Q(notes__note__icontains=r'other note')) 71 71 self.assertTrue(org_contact in qs) 72 72 73 def test_if_eval_false(self): 74 """ 75 Tests that generic relations work if the model instance evaluates to 76 false e.g(len(obj) == 0). 73 77 78 Test for bug http://code.djangoproject.com/ticket/13085. 74 79 80 It does not test that generic objects work if content_type_id=0 but it 81 would be nice if it did. 82 """ 83 # A group is created with no people as members. 84 # Since len(grp) == 0, due to no members, it will evaluate to False. 85 grp = GroupOfPeople(name='Those People') 86 grp.save() 87 note = Note.objects.create(note='They did it.', content_object=grp) 88 note.save() 89 # It would be nice to also test for adding a generic object with 90 # content_type_id=0. However there appears to be no content type 91 # with id=0 so it would be hard to make such a test. 92 -
tests/regressiontests/generic_relations_regress/models.py
4 4 5 5 __all__ = ('Link', 'Place', 'Restaurant', 'Person', 'Address', 6 6 'CharLink', 'TextLink', 'OddRelation1', 'OddRelation2', 7 'Contact', 'Organization', 'Note' )7 'Contact', 'Organization', 'Note', 'GroupOfPeople') 8 8 9 9 class Link(models.Model): 10 10 content_type = models.ForeignKey(ContentType) … … 77 77 name = models.CharField(max_length=255) 78 78 contacts = models.ManyToManyField(Contact, related_name='organizations') 79 79 80 81 # models for test_if_eval_false 82 class GroupOfPeople(models.Model): 83 name = models.CharField(max_length=100) 84 people = models.ManyToManyField(Person) 85 86 def __len__(self): 87 return self.people.all().count()