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" % self.name


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)" % (self.id, self.name)


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(default=timezone.now)

 
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 django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/751007f9-f6e5-4a41-88d0-e08f80995865%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment