admin管理员组文章数量:1389931
I am creating a kanban django model to my project, but i already tried a lot of things, i have read the django.docs, but i didn't find anything.
ERRORS: tasks_management.Account.projects: (fields.E339) 'AccountProjects.owner' is not a foreign key to 'Project'. tasks_management.AccountProjects: (fields.E336) The model is used as an intermediate model by 'tasks_management.Account.projects', but it does not have a foreign key to 'Account' or 'Project'.
from django.contrib.auth.models import User
from django.db import models
class Project(models.Model):
project_name = models.CharField(max_length=50)
def __str__(self):
return self.project_name
class Account(models.Model):
username = models.CharField(max_length=50)
email = models.EmailField(max_length=254)
password = models.CharField(max_length=30)
projects = models.ManyToManyField(
Project,
through='AccountProjects',
through_fields=('contributors', 'owner'),
blank=True
)
def __str__(self):
return self.username
class AccountProjects(models.Model):
owner = models.ForeignKey(Account, on_delete=models.CASCADE, related_name='owner_project')
contributors = models.ForeignKey(Account, on_delete=models.CASCADE, related_name='contributors_project')
#
# class Board(models.Model):
# project_name = models.ForeignKey(Project, on_delete=models.CASCADE)
#
# def __str__(self):
# return self.project_name.project_name
#
# class Column(models.Model):
# column_name = models.CharField(max_length=30)
# board_name = models.ForeignKey(Board, on_delete=models.CASCADE)
# order_position = models.IntegerField(default=1)
#
# def __str__(self):
# return self.column_name
#
# class Task(models.Model):
# task_name = models.CharField(max_length=50)
# description = models.TextField()
# creation_date = models.DateField(auto_created=True)
# updated_date = models.DateField(auto_now=True)
# column_name = models.ForeignKey(Column, on_delete=models.CASCADE)
#
# def __str__(self):
# return self.task_name
I read the docs, but i didn't find anything, the database requirements are:
a account doesn't need a project to be instantiated a project does need a account to be instantiated a project has 1 owner, and multiple contributors
a project has 1 board a board has multiple columns a task has 1 column
I am creating a kanban django model to my project, but i already tried a lot of things, i have read the django.docs, but i didn't find anything.
ERRORS: tasks_management.Account.projects: (fields.E339) 'AccountProjects.owner' is not a foreign key to 'Project'. tasks_management.AccountProjects: (fields.E336) The model is used as an intermediate model by 'tasks_management.Account.projects', but it does not have a foreign key to 'Account' or 'Project'.
from django.contrib.auth.models import User
from django.db import models
class Project(models.Model):
project_name = models.CharField(max_length=50)
def __str__(self):
return self.project_name
class Account(models.Model):
username = models.CharField(max_length=50)
email = models.EmailField(max_length=254)
password = models.CharField(max_length=30)
projects = models.ManyToManyField(
Project,
through='AccountProjects',
through_fields=('contributors', 'owner'),
blank=True
)
def __str__(self):
return self.username
class AccountProjects(models.Model):
owner = models.ForeignKey(Account, on_delete=models.CASCADE, related_name='owner_project')
contributors = models.ForeignKey(Account, on_delete=models.CASCADE, related_name='contributors_project')
#
# class Board(models.Model):
# project_name = models.ForeignKey(Project, on_delete=models.CASCADE)
#
# def __str__(self):
# return self.project_name.project_name
#
# class Column(models.Model):
# column_name = models.CharField(max_length=30)
# board_name = models.ForeignKey(Board, on_delete=models.CASCADE)
# order_position = models.IntegerField(default=1)
#
# def __str__(self):
# return self.column_name
#
# class Task(models.Model):
# task_name = models.CharField(max_length=50)
# description = models.TextField()
# creation_date = models.DateField(auto_created=True)
# updated_date = models.DateField(auto_now=True)
# column_name = models.ForeignKey(Column, on_delete=models.CASCADE)
#
# def __str__(self):
# return self.task_name
I read the docs, but i didn't find anything, the database requirements are:
a account doesn't need a project to be instantiated a project does need a account to be instantiated a project has 1 owner, and multiple contributors
a project has 1 board a board has multiple columns a task has 1 column
Share Improve this question asked Mar 14 at 13:41 MachareteMacharete 142 bronze badges 3 |1 Answer
Reset to default 0I think that many to many attribute/entity is expecting a Foreign Key to Project model, and that's what generating your error. Perhaps you may need to rethink your models a little. For example, a project will always have an owner and contributors, so you may add these attributes to Project model instead of Account, and that many to many field would be added in Project as well (a project has single owner and many contributors).
Then, if you want to get all project a single user is participating you could use "get_related" methods to pull that QuerySet.
本文标签: pythonError with ManyToManyField relation in DjangoStack Overflow
版权声明:本文标题:python - Error with ManyToManyField relation in Django - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744653306a2617816.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
AccountProjects
model contributors and owner both are linked withAccount
model for this reason you are getting the error – Google User Commented Mar 17 at 4:24ManyToManyField
setup, you're usingthrough_fields=('contributors', 'owner')
, but these fields must match the actual foreign keys inAccountProjects
. correct the field order here:python projects = models.ManyToManyField( Project, through='AccountProjects', through_fields=('project', 'account') )
– Google User Commented Mar 17 at 4:28