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
  • I'm not sure if contributors should be a foreign key, it will restrict you to have one single instance of account project with a single contributor and you could infer that with accounts that are listed in a single project (and that manytomany is already through projects attribute). Regarding your specific error, I don't think you linked AccountProjects to use a Project object as well. You could have an "owner" attribute in Project and contributors as part of that same attribute (many to many). – Héctor Asencio L Commented Mar 14 at 14:31
  • In your AccountProjects model contributors and owner both are linked with Account model for this reason you are getting the error – Google User Commented Mar 17 at 4:24
  • In your ManyToManyField setup, you're using through_fields=('contributors', 'owner'), but these fields must match the actual foreign keys in AccountProjects. correct the field order here: python projects = models.ManyToManyField( Project, through='AccountProjects', through_fields=('project', 'account') ) – Google User Commented Mar 17 at 4:28
Add a comment  | 

1 Answer 1

Reset to default 0

I 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