如何在Django中进行过滤查询? [英] How to make filter query in Django?
本文介绍了如何在Django中进行过滤查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下 model.py 。我想在输入期间创建一个过滤器。 SubCategory自动从成员Category中提取。子类别中的输入电流将显示所有内容而无需过滤器。我想选择一个父母,然后仅从父母的成员中选择。
I have the following model.py. I want to create a filter during Input. SubCategory automatically extracted from members Category. While the input current during subcategory will display the entire contents without filter. I want to choose one parent, then just out of the parent's members only.
> from django.db import models
>
> > class Category(models.Model):
> > name = models.CharField(max_length=100)
> >
> > def __unicode__(self):
> > return self.name
> >
> > class SubCategory(models.Model):
> > name = models.CharField(max_length=50)
> > category = models.ForeignKey(Category)
> >
> > def __unicode__(self):
> > return self.name
> >
> > class Input(models.Model):
> > name = models.CharField(max_length=30)
> > category = models.ForeignKey(Category)
> > subcategory = models.ForeignKey(SubCategory)
> >
> > def __unicode__(self):
> > return self.name
推荐答案
看看过滤器文档。
要获取一组类别由类别 my_cat
填充的子类别,您可以-
To get a group of subcategories whose category field is populated by the category my_cat
, you would do -
sub_cats = SubCategory.objects.filter(category=my_cat)
这篇关于如何在Django中进行过滤查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文