Ticket #30343: 30343-test.diff

File 30343-test.diff, 2.3 KB (added by Mariusz Felisiak, 5 years ago)
  • tests/prefetch_related/models.py

    commit faa8ee1bcf49d33f495804f16d9d03a389ae22ae
    Author: Mariusz Felisiak <felisiak.mariusz@gmail.com>
    Date:   Wed Apr 10 07:13:16 2019 +0200
    
        FIxed #30343
    
    diff --git a/tests/prefetch_related/models.py b/tests/prefetch_related/models.py
    index 091d600475..b55c92015a 100644
    a b class Comment(models.Model):  
    196196        ordering = ['id']
    197197
    198198
     199class PetUUID(models.Model):
     200    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
     201    name = models.CharField(max_length=20)
     202
     203
     204class CommentUUID(models.Model):
     205    comment = models.TextField()
     206
     207    # Content-object field
     208    content_type = models.ForeignKey(ContentType, models.CASCADE)
     209    object_pk = models.CharField(max_length=40)
     210    content_object = GenericForeignKey(ct_field="content_type", fk_field="object_pk")
     211
     212    class Meta:
     213        ordering = ['id']
     214
     215
    199216# Models for lookup ordering tests
    200217
    201218class House(models.Model):
  • tests/prefetch_related/tests.py

    diff --git a/tests/prefetch_related/tests.py b/tests/prefetch_related/tests.py
    index 24982dda14..ff377a28bb 100644
    a b from django.test.utils import CaptureQueriesContext  
    88
    99from .models import (
    1010    Author, Author2, AuthorAddress, AuthorWithAge, Bio, Book, Bookmark,
    11     BookReview, BookWithYear, Comment, Department, Employee, FavoriteAuthors,
    12     House, LessonEntry, ModelIterableSubclass, Person, Qualification, Reader,
    13     Room, TaggedItem, Teacher, WordEntry,
     11    BookReview, BookWithYear, Comment, CommentUUID, Department, Employee,
     12    FavoriteAuthors, House, LessonEntry, ModelIterableSubclass, Person,
     13    PetUUID, Qualification, Reader, Room, TaggedItem, Teacher, WordEntry,
    1414)
    1515
    1616
    class GenericRelationTests(TestCase):  
    885885            qs = Comment.objects.prefetch_related('content_object')
    886886            [c.content_object for c in qs]
    887887
     888    def test_prefetch_GFK_uuid_pk(self):
     889        pet = PetUUID.objects.create(name='Dog')
     890        CommentUUID.objects.create(comment='awesome', content_object=pet)
     891        qs = CommentUUID.objects.prefetch_related('content_object')
     892        self.assertEqual([c.content_object for c in qs], [pet])
     893
    888894    def test_traverse_GFK(self):
    889895        """
    890896        A 'content_object' can be traversed with prefetch_related() and
Back to Top