python - Django:如何使用 'limit_choices_to' 中的父表字段?

标签 python django

我有两个类SystemStation,它们继承自BaseBase 类具有 Group 的外键,Station 具有System 的外键。

我正在尝试对后一个 FK 施加“limit_choices_to”约束,以便相关系统必须来自与站相同的

这是 models.py 的片段

class Base(models.Model):
    group = models.ForeignKey(Group)

class System(Base):
    ...

class Station(Base):
    system_info = models.ForeignKey(System, limit_choices_to={'group': 'self.group'})

我尝试了很多想法,包括上面的想法,但没有成功。任何帮助都感激不尽!

最佳答案

system_info = models.ForeignKey(System, limit_choices_to=Q(groups__name= 'group'))

我希望这会起作用。群组将是您创建的群组名称。

关于python - Django:如何使用 'limit_choices_to' 中的父表字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43229835/

相关文章:

python - 将类型为 'object' 的数据帧列转换为类型列表

python - 在 matplotlib 中的图形对象之间切换 - 更改事件图形

Django Nginx 在浏览器上缓存静态文件

python - google CloudSQL mysql 表情符号(1366、 "Incorrect string value: '\\xF0\\x9F\\x98\\x80\\xF0\\x9F.. .' for column ' 名称'在第 1 行")

python - 在 Django 中访问静态文件时出现错误 404 和 500

Django - 重定向到登录页面与 403

python - 如何测试(使用 unittest)Django View 的 HTML 输出?

python - Django 循环模型依赖

python - DRF 使 DateTimeField 变得简单,而不考虑默认时区

django - Razorpay django 集成