Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何筛选多个字段的记录?_Python_Django_Django Models - Fatal编程技术网

Python 如何筛选多个字段的记录?

Python 如何筛选多个字段的记录?,python,django,django-models,Python,Django,Django Models,我有以下Django型号: class TopicLabel(models.Model): name = models.CharField(max_length=256) order = models.IntegerField(null=True, blank=True) topics = models.ManyToManyField(Topic, through='TopicLabelConnection') class Topic(models.Model):

我有以下Django型号:

class TopicLabel(models.Model):
    name = models.CharField(max_length=256)
    order = models.IntegerField(null=True, blank=True)
    topics = models.ManyToManyField(Topic, through='TopicLabelConnection')

class Topic(models.Model):
    title = models.CharField(max_length=140)
    visible = models.NullBooleanField(null=True, blank=True, default=False)

    def __unicode__(self):
        return self.title
    class Meta:
        verbose_name = _('topic')
        verbose_name_plural = _('topics')

class TopicLabelConnection(models.Model):
    topicId = models.ForeignKey(Topic, related_name='connection_topic')
    labelId = models.ForeignKey(TopicLabel, related_name='connection_label')

    def __unicode__(self):
        return self.labelId.name + ' / ' + self.topicId.title
我想创建一个
TopicLabel
方法,它将返回
TopicLabel.topic
集合中的所有主题,这些主题具有
topic.visible=True

我想编写一个类似于以下查询的Django程序:

SELECT *
FROM OPINIONS_TOPICLABELCONNECTION, OPINIONS_TOPIC
WHERE (OPINIONS_TOPICLABELCONNECTION.topicId_id = OPINIONS_TOPIC.id) AND
    (OPINIONS_TOPICLABELCONNECTION.labelId_id = X) AND 
    (OPINIONS_TOPIC.visible = 1)
其中
X
是主题标签的主键

我尝试了以下方法定义,但都失败了:

(一)

(二)

(三)

(四)

(五)


正确的代码是什么?

首先,您需要将
self
作为方法的第一个参数。然后过滤主题。试试这个:

class TopicLabel(models.Model):
    [...]
    def getVisibleTopics(self):
        return self.topics.filter(visible=True)
另外,是否有创建自定义直通表的原因?看起来您没有向其中添加任何额外数据

class TopicLabel(models.Model):
    [...]
    def getVisibleTopics():
        return topics.filter(visible=True)
class TopicLabel(models.Model):
    [...]
    def getVisibleTopics():
        return Topic.objects.filter(connection_label__visible=True).filter(connection_label__id=self.id)
class TopicLabel(models.Model):
    [...]
    def getVisibleTopics():
        return Topic.objects.filter(connection_label__visible=True).filter(connection_label__id=self.id)
class TopicLabel(models.Model):
    [...]
    def getVisibleTopics():
        return topics.filter(connection_topicId__visible=True)
class TopicLabel(models.Model):
    [...]
    def getVisibleTopics(self):
        return self.topics.filter(visible=True)