0
假设我已经按照我的模型类:查询在Django模型相关的对象集合
class User(BaseModel):
..
class Node(BaseModel):
..
author = models.ForeignKey(User, related_name='%(class)ss')
tags = models.ManyToManyField('Tag', related_name='%(class)ss')
..
class Tag(BaseModel):
..
而现在,我想有一个User
类中的方法,这将返回所有对于用户的节点标签:
class User(BaseModel):
..
def get_tags(self):
# Here some more querying for tags in selected nodes
user_tags = self.nodes.filter(author=self) # What to add if possible?
return ..
我想要user_tag
收集到包括在已经撰写由给定用户节点中使用的所有标签。如何实现这个没有自定义SQL?
我认为'select_related()'就像是预先加载相关的对象。我想要做的只是执行一些“选择”来检索这些相关的对象。让我们来说吧:'self.nodes.filter(author = self).tags'(你有想法?) – rafek