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

Cannot query using tags__name__in #889

Open
marcrleonard opened this issue Feb 19, 2024 · 1 comment
Open

Cannot query using tags__name__in #889

marcrleonard opened this issue Feb 19, 2024 · 1 comment

Comments

@marcrleonard
Copy link

Django Version: 4.2.9
Taggit Version: 5.0.1

Hello, I have been beating my head against this and cannot figure it out. I am using the through parameter to point to a UUID tag class. Here is my setup:

import uuid

from django.db import models
from django.utils.translation import gettext_lazy as _
from django.contrib.auth import get_user_model

from taggit.managers import TaggableManager
from taggit.models import GenericUUIDTaggedItemBase, TagBase

class AssetTag(TagBase):
    user = models.ForeignKey(
        get_user_model(),
        related_name="assettag",
        on_delete=models.CASCADE
    )
    class Meta:
        verbose_name = _("Tag")
        verbose_name_plural = _("Tags")

class AssetTaggedItem(GenericUUIDTaggedItemBase):
    tag = models.ForeignKey(
        AssetTag,
        related_name="assettaggeditem",
        on_delete=models.CASCADE
    )
    class Meta:
        verbose_name = _("Tag")
        verbose_name_plural = _("Tags")

class Asset(models.Model):
    id = models.CharField(primary_key=True, max_length=36, default=uuid.uuid4, editable=False, db_index=True)
    user_id = models.ForeignKey(
        get_user_model(),
        on_delete=models.CASCADE,
    )
    tags = TaggableManager(through=AssetTaggedItem)

I will create an asset and tag it with some_tag ... then I try to query assets with the tag like this:

_t = AssetTag.objects.filter(name='some_tag').values_list('name', flat=True)
assets = Asset.objects.filter(tags__name__in=_t).distinct()

or
assets = Asset.objects.filter(tags__name__in=["some_tag"]).distinct()

Both return no results. I cannot figure out whats going on.

@rtpg
Copy link
Contributor

rtpg commented Mar 7, 2024

First question: If you do AssetTag.objects.all(), do you see the tag you are expecting to find? Like you're sure the tag is saved? I'm assuming that this database is pretty small.

Second question: if you have a specific asset, does asset.tags.objects.all() give you the tags you're expecting in general?

Sometimes it is helpful to do things like:

print(Asset.objects.filter(tags__name__in=["some_tag"]).query)

This should help you see what exactly your system is doing.

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

2 participants