Here is my models.py
这是我的models.py
class Category(models.Model):
name = models.CharField(max_length=200, default='')
slug = models.SlugField(max_length=100,default='',unique=True)
def __unicode__(self):
return self.name
class SubCategory(models.Model):
category = models.ForeignKey(Category)
name = models.CharField(max_length=200, default='')
slug = models.SlugField(max_length=100,default='',unique=True)
class TutorInfo(models.Model):
user = models.OneToOneField(User)
name = models.CharField(max_length=255, default='')
category = models.ManyToManyField(Category, related_name='categories')
about = models.TextField(default='')
class Course(models.Model):
user = models.ForeignKey(User)
category = models.ForeignKey(Category)
sub_category = models.ForeignKey(SubCategory)
course_name = models.CharField(max_length=255, default='')
Now I want to filter the TutorInfo
such that sub_category
matches the sub_category
in their added Course
. A Tutor
can have more than one Course
. Is it possible to write such a query? If not then please advice me what alterations should I do in the models.py
现在我想过滤TutorInfo,使sub_category与添加的Course中的sub_category匹配。导师可以有多个课程。是否可以编写这样的查询?如果没有,那么请告诉我我应该在models.py中做些什么改动
1 个解决方案
#1
1
If 1 is the id of the sub_category you're interested in, use
如果1是您感兴趣的sub_category的id,请使用
TutorInfo.objects.filter(user__course__sub_category__id__exact=1)
whether if sc
is the sub_category model instance:
是否sc是sub_category模型实例:
TutorInfo.objects.filter(user__course__sub_category=sc)
#1
1
If 1 is the id of the sub_category you're interested in, use
如果1是您感兴趣的sub_category的id,请使用
TutorInfo.objects.filter(user__course__sub_category__id__exact=1)
whether if sc
is the sub_category model instance:
是否sc是sub_category模型实例:
TutorInfo.objects.filter(user__course__sub_category=sc)