0
我有兩個對象,公司和帳戶,採用不同的包。 他們通過員工擁有多對多關係,其中有一個額外字段is_admin。如何在django中的雙向多對多關係中從兩個模型中獲取集合
在公司中,我定義了關係,並在線閱讀,似乎我不必在Account中重新定義關係(這將導致循環導入)。
從CompanySerializer中檢索所有賬戶是沒有問題的, 但我需要能夠獲得所有註冊到賬戶的公司。
這是我的想法:
帳戶型號:
class Account(AbstractBaseUser):
current_jobs = models.ManyToManyField(
Company, through='Employee') // I need to define current_jobs in some way
//,but this results in circular import
公司型號:
class Company(models.Model):
employees = models.ManyToManyField(
settings.AUTH_USER_MODEL, through='Employee')
員工型號:
class Employee(models.Model):
class Meta:
unique_together = ('user', 'company')
user = models.ForeignKey(settings.AUTH_USER_MODEL,
on_delete=models.CASCADE)
company = models.ForeignKey(Company, on_delete=models.CASCADE)
is_admin = models.BooleanField()
的問題是,我怎麼會現在定義好w來獲取這兩個序列化器中的每個列表。 因此,公司串行器和帳戶串行器...
如果我不定義current_jobs,我收到一個錯誤,指出current_jobs沒有定義,它當然不是。
呵呵,我對django很陌生,不知道這個變量是如何命名的。我很抱歉問一個愚蠢的問題!應該找到了這個:)!但它確實解決了我的問題,所以謝謝! – Patidati