Filtering Queryset by Foreign Key Properties

6,045 views
Skip to first unread message

josch

unread,
Feb 11, 2011, 12:20:33 PM2/11/11
to Django users
Hi,

I`m searching for a solution for the following problem:

I have, for example, those two classes:

class Order(self):
type = models.CharField(1)

class OrderPositions(self):
order = ForeignKey(Order)


Now I want to get every Order Position, whoose order has the type, for
example 'A'.

queryset = OrderPositions.objects.filter(order.type='A')

Of course this does not work, I just wanted to make clear what I want
to to.

What is the best way to handle a demand like this.

Thank you!

Tom Evans

unread,
Feb 11, 2011, 1:06:17 PM2/11/11
to django...@googlegroups.com

http://docs.djangoproject.com/en/1.2/topics/db/queries/#lookups-that-span-relationships

queryset = OrderPositions.objects.filter(order__type='A')

Cheers

Tom

Axel Bock

unread,
Feb 11, 2011, 1:07:33 PM2/11/11
to django...@googlegroups.com
Hi,

try this, I guess this should workd (being new here myself :):
OrderPositions.objects.filter(order__type = "A")


and maybe have a look here:
http://docs.djangoproject.com/en/1.2/ref/models/querysets/#field-lookups


Cheers,
Axel.


2011/2/11 josch <josc...@web.de>

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.


josch

unread,
Feb 11, 2011, 1:10:20 PM2/11/11
to Django users
Thank you both!
Message has been deleted

Daniel Sears

unread,
Mar 31, 2018, 11:20:30 AM3/31/18
to django...@googlegroups.com
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 <jamals...@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...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages