我正在尝试使用ManyToMany关系,它给了我以下错误:
NameError: name 'ManyToManyField' is not defined
背景:构建一个用户拥有(很多)项目的应用程序,项目有(很多)任务列表和项目都有(很多)标记来表征它们。
这是我的models.py,非常感谢任何帮助捕获错误。
from django.db import models
from django.contrib.auth.models import User
#Not sure if I need the ID's. They may be superfluous. Ask in the lab today.
#Used to list collaborators on projects and track friends lists
#Borrowed from http://stackoverflow.com/a/1113039/1254402
class SeparatedValuesField(models.TextField):
__metaclass__ = models.SubfieldBase
def __init__(self, *args, **kwargs):
self.token = kwargs.pop('token', ',')
super(SeparatedValuesField, self).__init__(*args, **kwargs)
def to_python(self, value):
if not value: return
if isinstance(value, list):
return value
return value.split(self.token)
def get_db_prep_value(self, value):
if not value: return
assert (isinstance(value, list) or isinstance(value, tuple))
return self.token.join([unicode(s) for s in value])
def value_to_string(self, obj):
value = self._get_val_from_obj(obj)
class UserProfile(models.Model):
user_id = models.CharField(max_length=100, null=True, blank=True, unique=True)
user = models.OneToOneField(User)
# The additional attributes we wish to include.
first_name = models.CharField(max_length=64)
surname = models.CharField(max_length=64)
friends = SeparatedValuesField()
picture = models.ImageField(upload_to='profile_images', blank=True)
projects = models.ManyToManyField('Project')
# This line is required. Links UserProfile to a User model instance.
# Override the __unicode__() method to return out something meaningful!
def __unicode__(self):
return self.user.username
#Project model.
#Attributes = name, website, team members
#Many users can have many projects
class Project(models.Model):
project_id = models.CharField(max_length=100, null=True, blank=True, unique=True)
name = models.CharField(max_length=128)
description = models.CharField(max_length=300)
website = models.URLField(blank=True)
team_members = SeparatedValuesField()
class List(models.Model):
#Many lists belong to one project
project = models.ForeignKey(Project)
tasks = ManyToManyField('Task')
name = models.CharField(max_length=128)
colour = models.CharField(max_length=10)
#NOTE! - Abstracting the tags (category, progress and priority) to project level.
class Task(models.Model):
#Many tasks belong to one list
belong_to_list = models.ForeignKey(List)
#Unique id for task
task_id = models.CharField(max_length=100, null=True, blank=True, unique=True)
#Standard title & description
title = models.CharField(max_length=100)
description = models.CharField(max_length=300)
#NOTE! - Abstracting the tags (category, progress and priority) to project level.
class Tag(models.Model):
project = models.ForeignKey(Project)
#UUID
task_id = models.CharField(max_length=100, null=True, blank=True, unique=True)
#User assigns each tag a colour. hex code.
colour = models.CharField(max_length=10)
答案 0 :(得分:3)
在List
中,您指的是ManyToManyField
而未通过models
引用它,就像您对所有其他字段一样。
请注意,错误消息会告诉您问题的确切位置,因此您无需发布所有其他不相关的代码。