Ticket #13085: 13085.diff

File 13085.diff, 3.4 KB (added by Ben, 15 years ago)

Patch with tests included

  • django/contrib/contenttypes/generic.py

     
    4949        # Convenience function using get_model avoids a circular import when
    5050        # using this model
    5151        ContentType = get_model("contenttypes", "contenttype")
    52         if obj:
     52        if obj is not None:
    5353             return ContentType.objects.db_manager(obj._state.db).get_for_model(obj)
    54         elif id:
     54        elif id is not None:
    5555             return ContentType.objects.db_manager(using).get_for_id(id)
    5656        else:
    5757            # This should never happen. I love comments like this, don't you?
     
    7272            # performance when dealing with GFKs in loops and such.
    7373            f = self.model._meta.get_field(self.ct_field)
    7474            ct_id = getattr(instance, f.get_attname(), None)
    75             if ct_id:
     75            if ct_id is not None:
    7676                ct = self.get_content_type(id=ct_id, using=instance._state.db)
    7777                try:
    7878                    rel_obj = ct.get_object_for_this_type(pk=getattr(instance, self.fk_field))
  • tests/regressiontests/generic_relations_regress/tests.py

     
    7070            Q(notes__note__icontains=r'other note'))
    7171        self.assertTrue(org_contact in qs)
    7272
     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).
    7377
     78        Test for bug http://code.djangoproject.com/ticket/13085.
    7479
     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

     
    44
    55__all__ = ('Link', 'Place', 'Restaurant', 'Person', 'Address',
    66           'CharLink', 'TextLink', 'OddRelation1', 'OddRelation2',
    7            'Contact', 'Organization', 'Note')
     7           'Contact', 'Organization', 'Note', 'GroupOfPeople')
    88
    99class Link(models.Model):
    1010    content_type = models.ForeignKey(ContentType)
     
    7777    name = models.CharField(max_length=255)
    7878    contacts = models.ManyToManyField(Contact, related_name='organizations')
    7979
     80
     81# models for test_if_eval_false
     82class 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()
Back to Top