[Django]-Django filter queryset by attribute subclass

0👍

If your Slug model defines the association with Pages in the following way:

class Slug(models.Model):
...
    content_type = models.ForeignKey(ContentType)
    object_id = models.PositiveIntegerField()
    page = generic.GenericForeignKey('content_type', 'object_id')

Then, your query should look something like this:

content_type = ContentType.objects.get_for_model(BlogPost)
Slug.objects.filter(name=slug_name, content_type__pk=content_type.id)
👤OrenD

0👍

Haven’t tested this but give it a try:

Slug.objects.filter(name=slug_name, blogpost__isnull=False)
👤Todor

Leave a comment