Ticket #17991: generic_foreign_key_char_test.diff

File generic_foreign_key_char_test.diff, 2.4 KB (added by carmandrew@…, 12 years ago)
  • tests/modeltests/prefetch_related/models.py

    diff --git a/tests/modeltests/prefetch_related/models.py b/tests/modeltests/prefetch_related/models.py
    index 85488f0..d688800 100644
    a b class TaggedItem(models.Model):  
    125125                                      related_name='taggeditem_set3')
    126126    created_by_fkey = models.PositiveIntegerField(null=True)
    127127    created_by = generic.GenericForeignKey('created_by_ct', 'created_by_fkey',)
     128    favorite_ct = models.ForeignKey(ContentType, null=True,
     129                                      related_name='taggeditem_set4')
     130    favorite_fkey = models.CharField(max_length=64, null=True)
     131    favorite = generic.GenericForeignKey('favorite_ct', 'favorite_fkey',)
    128132
    129133    def __str__(self):
    130134        return self.tag
    class TaggedItem(models.Model):  
    132136
    133137class Bookmark(models.Model):
    134138    url = models.URLField()
    135     tags = generic.GenericRelation(TaggedItem)
     139    tags = generic.GenericRelation(TaggedItem, related_name='bookmarks')
     140    favorite_tags = generic.GenericRelation(TaggedItem,
     141                                    content_type_field='favorite_ct',
     142                                    object_id_field='favorite_fkey',
     143                                    related_name='favorite_bookmarks')
    136144
    137145
    138146class Comment(models.Model):
  • tests/modeltests/prefetch_related/tests.py

    diff --git a/tests/modeltests/prefetch_related/tests.py b/tests/modeltests/prefetch_related/tests.py
    index 614a5fc..e81560f 100644
    a b class GenericRelationTests(TestCase):  
    319319                    for t in b.tags.all()]
    320320            self.assertEqual(sorted(tags), ["django", "python"])
    321321
     322    def test_charfield_GFK(self):
     323        b = Bookmark.objects.create(url='http://www.djangoproject.com/')
     324        t1 = TaggedItem.objects.create(content_object=b, tag='django')
     325        t2 = TaggedItem.objects.create(content_object=b, favorite=b, tag='python')
     326
     327        with self.assertNumQueries(3):
     328            bookmark = Bookmark.objects.filter(pk=b.pk).prefetch_related('tags', 'favorite_tags')[0]
     329            self.assertEqual(sorted([i.tag for i in bookmark.tags.all()]), ["django", "python"])
     330            self.assertEqual([i.tag for i in bookmark.favorite_tags.all()], ["python"])
     331
    322332
    323333class MultiTableInheritanceTest(TestCase):
    324334
Back to Top