Saturday, March 31, 2018

Filtering Queryset by Foreign Key Properties

class ProductType(models.Model):

image = models.ImageField(upload_to='products_type_images/')
name = models.CharField(max_length=20, blank=True, null=True, default=None)
is_active = models.BooleanField(default=True)

def __str__(self):
return "%s" %

class Product(models.Model):

name = models.CharField(max_length=20)
price = models.DecimalField(max_digits=10, decimal_places=2, default=0)
short_description = models.TextField(blank=True, null=True, default=None, max_length=100)
description = models.TextField(blank=True, null=True, default=None)
is_active = models.BooleanField(default=True)
type = models.ForeignKey(ProductType, blank=True, null=True, default=None)

def __str__(self):
return "(%s, %s)" % (,

class ProductImage(models.Model):

product = models.ForeignKey(Product, blank=True, null=True, default=None)
image = models.ImageField(upload_to='products_images/')
is_main = models.BooleanField(default=False)
is_active = models.BooleanField(default=True)
created = models.DateTimeField(

def category(request):
query = request.GET.get("q")
if query:
          products_images_list ProductImage.objects.filter(product__type__name__contains=query) 

But in my template this is not working I want to filter my product's types. Can you help my? Django 1.11

Thank you.

You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
To post to this group, send email to
Visit this group at
To view this discussion on the web visit
For more options, visit

No comments:

Post a Comment