Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug with prefetch_related #490

Closed
Uninen opened this issue May 14, 2017 · 5 comments
Closed

Bug with prefetch_related #490

Uninen opened this issue May 14, 2017 · 5 comments

Comments

@Uninen
Copy link

Uninen commented May 14, 2017

Posted this few weeks ago to the mailinglist, but seems that it's dead so re-posting here. (Maybe it would be a good idea to retire the mailinglist or post a notification there that it's not been actively followed?)

So, I have a following model with tags:

tags = TaggableManager(through='knuckles.UUIDTaggedItem', blank=True)

where UUIDTaggedItem is:

class UUIDTaggedItem(GenericUUIDTaggedItemBase, TaggedItemBase):

    class Meta:
        verbose_name = "Tag"
        verbose_name_plural = "Tags"

When I query for the tags I get following:

In [18]: e = LiveEvent.objects.get(slug='dj-tony-004')
In [19]: e.tags.all()

Out[19]: <QuerySet [<Tag: 90s>, <Tag: dj tony>, <Tag: house>, <Tag: radioshow>, <Tag: 00's>, <Tag: trance>, <Tag: 90's>, <Tag: eurodance>]>


In [20]: e = LiveEvent.objects.prefetch_related('tags').get(slug='dj-tony-004')
In [21]: e.tags.all()

Out[21]: <QuerySet []>

So when using prefetch_related, I always get empty tags. Not sure if I'm doing something wrong here or if this is a bug?

I'm using Django version 1.11 and django-taggit version 0.22.1

@Uninen Uninen changed the title Bug with fetch_related Bug with prefetch_related May 14, 2017
@imhmdb
Copy link

imhmdb commented Dec 25, 2019

I can confirm that this bug still exists almost 4 years after posting the issue, I ended up creating my own tags implementation because prefetching was very important for my use-case, I published a gist explaining how I solved my issue so i'll post it here to help anyone who might stumble upon it while facing the same issue.

https://gist.github.com/imhmdb/af57c9fc4db69bfdfdb900810744e678

@ghost
Copy link

ghost commented Feb 12, 2020

This occurs because instance_attr value isn't match _prefetch_related_val.
It worked fine by modifying as below. (only MySQL, SQLite3)

            if issubclass(self.through, GenericUUIDTaggedItemBase):
                instance_attr = lambda obj: obj._get_pk_val().hex
            else:
                instance_attr = lambda obj: obj._get_pk_val()

            return (
                qs,
                attrgetter("_prefetch_related_val"),
                instance_attr,
                False,
                self.prefetch_cache_name,
                False,
            )

@jdufresne
Copy link
Member

Thanks for looking into this. A PR that contains a test is welcome and will be reviewed.

@Uninen
Copy link
Author

Uninen commented Feb 20, 2020

Thanks a lot for the patch and the tests, @fumuumuf ! 👍

@Uninen
Copy link
Author

Uninen commented Feb 26, 2020

This is now fixed with #646

Thank You everyone for your work, waited almost 3 years for this :)

@Uninen Uninen closed this as completed Feb 26, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants