繁体   English   中英

django"<user: user556> " 在使用这种多对多关系之前,"id" 字段需要有一个值</user:>

[英]django "<User: user556>" needs to have a value for field "id" before this many-to-many relationship can be used

我正在尝试保存 M2M object (将其设置为默认值,即 id = 1 的值)。

我收到以下错误的问题:

"<User: user556>" 需要有一个字段 "id" 的值才能使用这种多对多关系。

我查看了涵盖相同错误的先前提要,但没有人解释它发生在 form_valid function 中,因为它发生在我身上

我的代码如下:

  def form_valid(self,form):

    instance = form.instance
    instance.groups.set(1)
    instance.save()
    return super(UserCreateView,self).form_valid(form)

您首先需要保存您的实例,使其具有主键,否则您无法在联结表中创建记录。

因此,您可以通过以下方式实现:

from django.http import HttpResponseRedirect

    def form_valid(self,form):
        instance = form.save()
        instance.groups.set(1)
        return HttpResponseRedirect(self.get_success_url())

此处返回HttpResponseRedirect是必要的,因为super().form_valid()会将表单再次保存到数据库中,并且还会将groups设置为表单中指定的组。

Django 属性错误:<object> 在使用这种多对多关系之前,需要为字段“id”提供一个 hvalue<div id="text_translate"><p> <strong>我的问题:</strong></p><p> 我正在尝试通过我的 django 项目的管理路径将一些模型添加到我的数据库中,但是当我尝试创建 model 的实例时,此错误不断弹出。 clean的 function 的存在是为了确保每个 Pokemon model 最多只能从所有可能的类型列表中选择 2 种类型。</p><p> <strong>我的代码:</strong></p><pre> from django.db import models from django.core.validators import MinValueValidator as min, MaxValueValidator as max from django.core.exceptions import ValidationError class PokeType(models.Model): poke_type = models.CharField(max_length=15) def __str__(self): return self.poke_type #Current generation of games for gen_added field gen = 8 class Pokemon(models.Model): poke_name = models.CharField(max_length=30) poke_type = models.ManyToManyField(PokeType, blank=True, null=True) evolves_from = False evolves_into = False gen_added = models.PositiveIntegerField(validators=[min(1), max(gen)]) def clean(self): #Allow max of 2 poke_types to be selected if self.poke_type.count() &gt; 2: raise ValidationError('A Pokemon has a maximum of two types.') class Meta: verbose_name_plural = 'Pokemon' abstract = True class CustomPokemon(Pokemon): name = models.CharField(max_length=30) level = models.PositiveIntegerField(blank=True, null=True) def __str__(self): return self.name</pre><p> <strong>我(有点)知道的:</strong></p><p> 根据我从其他人那里看到的问题,我需要先保存我的 model 的实例,然后才能在clean的 function 中使用多对多关系。 此外,当我的clean function 被注释掉时,我没有收到错误,因此我已将问题缩小到代码的那部分。</p><p> <strong>我试过的:</strong></p><p> 我认为更改我的 if 语句以检查poke_type变量的存在会有所帮助 - 即</p><pre> def clean(self): #Allow max of 2 poke_types to be selected if self.poke_type and self.poke_type.count() &gt; 2: raise ValidationError('A Pokemon has a maximum of two types.')</pre><p> 但错误仍然存在。 正如我上面提到的,删除 function 也允许我在不引发该特定错误的情况下继续进行,但会破坏我限制可选类型的最大数量的能力。</p><p> 接下来,我尝试创建一个save function 来保存 model 然后给 model 之后的poke_type属性:</p><pre> def save(self): self.save() self.poke_type = models.ManyToManyField(PokeType, blank=True, null=True)</pre><p> 有没有办法保存 model 的实例,因为它是通过管理站点添加的,还是不可能的?</p></div></object>

[英]Django AttributeError: <Object> needs to have a hvalue for field “id” before this many-to-many relationship can be used

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Django中可以使用多对多关系之前,对象需要具有字段“id”的值 Django:“ &lt;…&gt;”必须具有字段“ id”的值,然后才能使用这种多对多关系 Django:出现错误说“在使用这种多对多关系之前需要为字段“id”设置值” Django 属性错误:<object> 在使用这种多对多关系之前,需要为字段“id”提供一个 hvalue<div id="text_translate"><p> <strong>我的问题:</strong></p><p> 我正在尝试通过我的 django 项目的管理路径将一些模型添加到我的数据库中,但是当我尝试创建 model 的实例时,此错误不断弹出。 clean的 function 的存在是为了确保每个 Pokemon model 最多只能从所有可能的类型列表中选择 2 种类型。</p><p> <strong>我的代码:</strong></p><pre> from django.db import models from django.core.validators import MinValueValidator as min, MaxValueValidator as max from django.core.exceptions import ValidationError class PokeType(models.Model): poke_type = models.CharField(max_length=15) def __str__(self): return self.poke_type #Current generation of games for gen_added field gen = 8 class Pokemon(models.Model): poke_name = models.CharField(max_length=30) poke_type = models.ManyToManyField(PokeType, blank=True, null=True) evolves_from = False evolves_into = False gen_added = models.PositiveIntegerField(validators=[min(1), max(gen)]) def clean(self): #Allow max of 2 poke_types to be selected if self.poke_type.count() &gt; 2: raise ValidationError('A Pokemon has a maximum of two types.') class Meta: verbose_name_plural = 'Pokemon' abstract = True class CustomPokemon(Pokemon): name = models.CharField(max_length=30) level = models.PositiveIntegerField(blank=True, null=True) def __str__(self): return self.name</pre><p> <strong>我(有点)知道的:</strong></p><p> 根据我从其他人那里看到的问题,我需要先保存我的 model 的实例,然后才能在clean的 function 中使用多对多关系。 此外,当我的clean function 被注释掉时,我没有收到错误,因此我已将问题缩小到代码的那部分。</p><p> <strong>我试过的:</strong></p><p> 我认为更改我的 if 语句以检查poke_type变量的存在会有所帮助 - 即</p><pre> def clean(self): #Allow max of 2 poke_types to be selected if self.poke_type and self.poke_type.count() &gt; 2: raise ValidationError('A Pokemon has a maximum of two types.')</pre><p> 但错误仍然存在。 正如我上面提到的,删除 function 也允许我在不引发该特定错误的情况下继续进行,但会破坏我限制可选类型的最大数量的能力。</p><p> 接下来,我尝试创建一个save function 来保存 model 然后给 model 之后的poke_type属性:</p><pre> def save(self): self.save() self.poke_type = models.ManyToManyField(PokeType, blank=True, null=True)</pre><p> 有没有办法保存 model 的实例,因为它是通过管理站点添加的,还是不可能的?</p></div></object> <Post: mi post> ”必须具有字段“ id”的值,然后才能使用这种多对多关系 ValueError:在使用此多对多关系之前,需要为字段“ id”具有一个值 收到错误“<team: jaja> " 在使用这种多对多关系之前,"id" 字段需要有一个值</team:> Django错误:在可以使用多对多关系之前,需要为字段“...”设置一个值 Django CreateView给出了一个错误“需要有一个字段值”......“才能使用这种多对多关系。” Django:我收到“ValueError:在使用这种多对多关系之前需要字段“id”的值。”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM