Saturday, March 31, 2018

Re: Filtering Queryset by Foreign Key Properties

You may want to first write a test for this using something like pytest. That's what I did and the following worked fine. Note that query is a queryset, which is an iterable. So you have to index it. And product__type__name will ultimately match with a string because the name field in ProductType is a CharField.

query = ProductImage.objects.filter(created  = 'green')                                       
products_images_list = ProductImage.objects.filter(product__type__name__contains=query[0].product.type)  


On Fri, Mar 30, 2018 at 11:23 AM, Jamaldin Pro <jamalsema2017@gmail.com> wrote:
class ProductType(models.Model):
class Meta:
verbose_name_plural = 'тип'
verbose_name = 'типы'

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):
class Meta:
verbose_name_plural = 'запчасть'
verbose_name = 'запчасти'

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):
class Meta:
verbose_name_plural = 'фотография_запчасти'
verbose_name = 'фотографии_запчасти'

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, category_name):
# products_images_list = get_object_or_404(Product, id=category_name)
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/b501d828-0876-4bff-9eca-b667e11898fe%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
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/CALLzXtTE6BfXpzEzAT%2Brj%3DHiVOTZ0g_Q1cXkxPwf3nHVz0Q-PA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment