django models Values 错误阻止我创建一个模型,其中指定的字段作为参数?e
django models Values error preventing me from creating a model with fields specified as parameters?e
我似乎不明白这个错误或如何修复它。关于 django 模型,我有些不明白。
考虑一下当我尝试在 Keyword
模型上执行 get_or_create
时会发生什么。这是模型,以及我在 shell.
中编写的一些代码
@python_2_unicode_compatible
class Keyword(models.Model):
word = models.CharField(max_length=200)
statement = models.ManyToManyField(Statement)
def __str__(self):
return self.word
>>> from gtr_site.models import *
>>> a = Statement()
>>> Keyword.objects.get_or_create(word="Testkeyword", statement=a)
Traceback (most recent call last): ...
ValueError: "<Keyword: Testkeyword>" needs to have a value for field "keyword" before this many-to-many relationship can be used.
但是如果你简单地写Keyword.objects.get_or_create(word="TestKeyWord")
(所以如果你完全排除语句实例),那么错误就消失了。
我真的不明白这个错误是怎么发生的,因为... Statement
模型和 Keyword
模型实际上都没有名为 "keyword."[=27 的字段=]
但是,Statement
模型确实有很多组件。这是它的代码。
@python_2_unicode_compatible
class Statement(models.Model):
statement_id = models.CharField(max_length=200)
title = models.CharField(max_length=200)
issue_date = models.DateField("Issue-Date")
author = models.ForeignKey(Person)
released_by = models.ForeignKey(Organization)
keywords = models.ManyToManyField('KeywordInContext')
solokeywords = models.ManyToManyField('Keyword', related_name='statement_keywords')
为了清楚起见,我选择排除模型中的另外三个选择字段。
Statements
模型中只有一个字段 确实有 一个名为关键字的字段。与KeywordInContext
建立多对多关系的字段"keywords"这个模型如下:
@python_2_unicode_compatible
class KeywordInContext(models.Model):
keyword = models.ForeignKey(Keyword)
contexts = models.ManyToManyField(Keyword, related_name='keyword_context')
def __str__(self):
return self.keyword.word + ' (' + ', '.join(c.word for c in self.contexts.all()) + ')'
需要注意的重要一点是,字段关键字为 Keyword
对象创建外键。
所以...我还是不明白这是怎么回事。当我尝试创建同时具有 word
和 statement
字段作为参数的新关键字时,我不明白为什么 KeyInContext 中的字段变得相关。关于这一点,我如何创建一个 Keyword
对象,同时指定了 word
和 statement
参数?
该错误试图告诉您,您需要先保存关键字对象,然后才能通过多对多 link 对其进行访问。您还需要保存声明;这是因为 m2m 关系涉及 linking table,所以双方都需要 ID,然后才能 link。换句话说,您不能在 create
调用中分配 m2m 字段。
至于您收到该特定消息的原因,我猜您在内存中有一个旧版本的模型,您在其中将主键字段定义为 keyword
。
我似乎不明白这个错误或如何修复它。关于 django 模型,我有些不明白。
考虑一下当我尝试在 Keyword
模型上执行 get_or_create
时会发生什么。这是模型,以及我在 shell.
@python_2_unicode_compatible
class Keyword(models.Model):
word = models.CharField(max_length=200)
statement = models.ManyToManyField(Statement)
def __str__(self):
return self.word
>>> from gtr_site.models import *
>>> a = Statement()
>>> Keyword.objects.get_or_create(word="Testkeyword", statement=a)
Traceback (most recent call last): ...
ValueError: "<Keyword: Testkeyword>" needs to have a value for field "keyword" before this many-to-many relationship can be used.
但是如果你简单地写Keyword.objects.get_or_create(word="TestKeyWord")
(所以如果你完全排除语句实例),那么错误就消失了。
我真的不明白这个错误是怎么发生的,因为... Statement
模型和 Keyword
模型实际上都没有名为 "keyword."[=27 的字段=]
但是,Statement
模型确实有很多组件。这是它的代码。
@python_2_unicode_compatible
class Statement(models.Model):
statement_id = models.CharField(max_length=200)
title = models.CharField(max_length=200)
issue_date = models.DateField("Issue-Date")
author = models.ForeignKey(Person)
released_by = models.ForeignKey(Organization)
keywords = models.ManyToManyField('KeywordInContext')
solokeywords = models.ManyToManyField('Keyword', related_name='statement_keywords')
为了清楚起见,我选择排除模型中的另外三个选择字段。
Statements
模型中只有一个字段 确实有 一个名为关键字的字段。与KeywordInContext
建立多对多关系的字段"keywords"这个模型如下:
@python_2_unicode_compatible
class KeywordInContext(models.Model):
keyword = models.ForeignKey(Keyword)
contexts = models.ManyToManyField(Keyword, related_name='keyword_context')
def __str__(self):
return self.keyword.word + ' (' + ', '.join(c.word for c in self.contexts.all()) + ')'
需要注意的重要一点是,字段关键字为 Keyword
对象创建外键。
所以...我还是不明白这是怎么回事。当我尝试创建同时具有 word
和 statement
字段作为参数的新关键字时,我不明白为什么 KeyInContext 中的字段变得相关。关于这一点,我如何创建一个 Keyword
对象,同时指定了 word
和 statement
参数?
该错误试图告诉您,您需要先保存关键字对象,然后才能通过多对多 link 对其进行访问。您还需要保存声明;这是因为 m2m 关系涉及 linking table,所以双方都需要 ID,然后才能 link。换句话说,您不能在 create
调用中分配 m2m 字段。
至于您收到该特定消息的原因,我猜您在内存中有一个旧版本的模型,您在其中将主键字段定义为 keyword
。