django的mtpp打破了很多过滤器吗?

时间:2015-11-12 10:30:30

标签: django django-mptt

我有以下模型:

class Topic(MPTTModel):
    parent = TreeForeignKey('self', null=True, blank=True, related_name='children', db_index=True)  
    name = models.CharField(max_length=200, blank=False)
    tasks = models.ManyToManyField("Task", through="TopicTask")

class Task(models.Model):
    name = models.CharField(max_length=200, blank=False)
    madocs = models.ManyToManyField("Madoc", through="TaskMadoc")   

class TopicTask(models.Model):
    topic = models.ForeignKey("Topic")
    task = models.ForeignKey("Task")


class TaskMadoc(models.Model):
    task = models.ForeignKey("Task")
    madoc = models.ForeignKey("Madoc")

我试过

Task.objects.filter(madocs__id=1)

效果很好!

但是当我尝试

Topic.objects.filter(tasks__id=1)

错误是:

  

FieldError:无法将关键字“任务”解析为字段。选择是:   孩子,创建,id,级别,lft,名称,顺序,父母,parent_id,   rght,studyplan,topictask,tree_id

所以,选择没有任务!

为什么呢?是不是很麻烦?

1 个答案:

答案 0 :(得分:1)

Try

Topic.objects.filter(topictask__task__id=1)