Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django AttributeError:<;对象>;“字段”需要有一个hvalue;id";在此之前,可以使用多对多关系_Python_Django - Fatal编程技术网

Python Django AttributeError:<;对象>;“字段”需要有一个hvalue;id";在此之前,可以使用多对多关系

Python Django AttributeError:<;对象>;“字段”需要有一个hvalue;id";在此之前,可以使用多对多关系,python,django,Python,Django,我的问题: 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

我的问题:

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() > 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
我试图通过django项目上的管理路径将一些模型添加到我的数据库中,但是当我尝试创建模型的实例时,这个错误会不断出现。
clean
功能用于确保每个口袋妖怪模型最多只能从所有可能类型的列表中选择2种类型

我的代码:

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() > 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
我(某种程度上)知道的:

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() > 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
根据我从其他人那里看到的问题,我需要先保存模型的实例,然后才能在
clean
函数中使用多对多关系。另外,当我的
clean
函数被注释掉时,我没有得到错误,因此我将问题缩小到代码的这一部分

我所尝试的:

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() > 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
我认为更改if语句以检查
poke_类型的存在将有所帮助-即

     def clean(self):
         #Allow max of 2 poke_types to be selected
         if self.poke_type and self.poke_type.count() > 2:
             raise ValidationError('A Pokemon has a maximum of two types.')
但错误仍然存在。如上所述,删除该函数也允许我在不引发特定错误的情况下继续操作,但会破坏我限制最大可选择类型数的能力

接下来,我尝试创建一个
save
函数来保存模型,然后为模型赋予
poke_type
属性:

    def save(self):
        self.save()
        self.poke_type = models.ManyToManyField(PokeType, blank=True, null=True)

在通过管理站点添加模型实例时,是否有办法保存该实例?或者不可能?请改用
m2m\u changed
信号

def poke_type_changed(sender, **kwargs):
    if kwargs['instance'].poke_type.count() > 2:
        raise ValidationError('A Pokemon has a maximum of two types.')


m2m_changed.connect(poke_type_changed, sender=Pokemon.poke_type.through)

好的,我导入了
m2m\u changed
信号,但是当我按照您键入的方式写出代码时,它会引发一个
NameError:name Pokemon未定义
。我尝试缩进
m2m\u changed…
行,因此它是函数的一部分,允许代码运行而不给我NameError,但它也起到了类似于限制器不存在的作用。你建议的代码应该是口袋妖怪模型的一部分,对吗?好吧,我想出来了。我在模型
Pokemon
中定义函数,但在我将函数定义和信号调用移到模型定义之外和之后,它似乎起了作用。谢谢你提供的信息!