python - Error with ManyToManyField relation in Django - Stack Overflow

I am creating a kanban django model to my project, but i already tried a lot of things, i have read the

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744653306a4586032.html

相关推荐

  • python - Error with ManyToManyField relation in Django - Stack Overflow

    I am creating a kanban django model to my project, but i already tried a lot of things, i have read the

    1天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信
['keyword'] : $thread['subject']; $header['description'] = $thread['description'] ? $thread['description'] : $thread['brief']; $_SESSION['fid'] = $fid; if ($ajax) { empty($conf['api_on']) and message(0, lang('closed')); $apilist['header'] = $header; $apilist['extra'] = $extra; $apilist['access'] = $access; $apilist['thread'] = well_thread_safe_info($thread); $apilist['thread_data'] = $data; $apilist['forum'] = $forum; $apilist['imagelist'] = $imagelist; $apilist['filelist'] = $thread['filelist']; $apilist['threadlist'] = $threadlist; message(0, $apilist); } else { include _include(theme_load('single_page', $fid)); } break; default: message(-1, lang('data_malformation')); break; } ?>