【问题标题】:How to access another app model in django?如何在 django 中访问另一个应用程序模型?
【发布时间】:2018-07-25 16:21:10
【问题描述】:

我有两个应用程序 main 和 table 在我的主要我有一个模型 UserSelect 并且在表中是一个模型 Bowler 我需要这样做

from main.models import UserSelect, User

class Bowlers(models.Model):
 users = models.ManyToManyField(User, through='UserSelect')

但它给出的错误是 “字段通过模型'UserSelect'指定多对多关系,尚未安装” 那我该怎么做呢?

【问题讨论】:

    标签: django django-models many-to-many


    【解决方案1】:

    您可以通过使用字符串引用任何应用程序对模型进行延迟引用

    class Bowlers(models.Model):
        users = models.ManyToManyField('main.User', through='main.UserSelect')
    

    但是您遇到的具体问题是您应该将 UserSelect 类作为 through 参数而不是作为字符串传递

    class Bowlers(models.Model):
        users = models.ManyToManyField(User, through=UserSelect)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-09
      • 2011-12-22
      • 1970-01-01
      • 2015-10-25
      • 1970-01-01
      • 1970-01-01
      • 2019-05-17
      • 2021-08-02
      相关资源
      最近更新 更多