晚上好,各位,我的项目中有以下模型结构:
`
class Foo(models.Model):
foo_name = models.TextField()
foo_city = models.TextField()
foo_actions = models.ManyToManyField(Action, through="FooActions")
@property
def bar(self):
response = True if FooAction.objects.filter(
foo_id=self.pk,
foo__action=Action.ACTION_NAME_PK).first() else False
return response
`
我需要把“bar”字段放在过滤器里,但我不明白。
我试了如下:
Foo.objects.filter(nome="João").values("foo_city","bar")
有可能以这种方式带来这些信息吗?
对不起,我的英语很差。
发布于 2022-11-09 20:43:12
不,这是不可能的,因为@property方法与模型的对象交互--它不是模型的字段
模型字段是数据表的一列,但是@property方法与模型列交互,我的意思是它不是SQL查询的一部分
但是你可以通过第三方库来实现这些。请参阅此PyPI包链接这里
https://stackoverflow.com/questions/74381973
复制