Django:如何过滤我的模型(由孩子)?

时间:2022-04-11 19:33:38

For my university project, I've described several models for e-commerce of goods:

对于我的大学项目,我已经描述了几种商品电子商务模型:

Item, Book(Item), Stationery(Item), and ItemImage (related by ForeignKey for all Item-like models).

Item,Book(Item),Stationery(Item)和ItemImage(ForeignKey与所有类似Item的模型相关)。

I need to filter the set of item images in the following way:

我需要按以下方式过滤项目图像集:

def home(request):
   goods_images = ItemImage.objects.filter(is_active=True, is_main=True)
   goods_images_books = ItemImage.objects.filter(is_active=True, 
                                                 is_main=True)
   goods_images_stationeries = ItemImage.objects.filter(is_active=True, 
                                                        is_main=True)
return render(request, 'landing/home.html', locals())

The question is what the additional parameter I should add to filter()? Or is there another way of solving this problem?

问题是我应该为filter()添加额外的参数?还是有另一种解决这个问题的方法吗?

1 个解决方案

#1


0  

class Entry(models.Model):

    entry_title = models.ForeignKey(Title, on_delete=models.CASCADE)
    some_other_field= models.CharField()

class Title(models.Model):
    pass

Title.objects.filter(entry__some_other_field = 'something')

#1


0  

class Entry(models.Model):

    entry_title = models.ForeignKey(Title, on_delete=models.CASCADE)
    some_other_field= models.CharField()

class Title(models.Model):
    pass

Title.objects.filter(entry__some_other_field = 'something')