Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/19.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/4/algorithm/12.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
为什么Django自定义验证程序失败?_Django_Validation_Django Admin_Many To Many - Fatal编程技术网

为什么Django自定义验证程序失败?

为什么Django自定义验证程序失败?,django,validation,django-admin,many-to-many,Django,Validation,Django Admin,Many To Many,我正在尝试设计一个模型,允许我安排活动(垒球比赛)并从较大的团队名册中创建比赛名册: class Game(models.Model): DateTime = models.DateTimeField() opponent = models.CharField(max_length = 50, default="TBD") players = models.ManyToManyField(Player, limit_choices_to={'id__in': Player.objects.fil

我正在尝试设计一个模型,允许我安排活动(垒球比赛)并从较大的团队名册中创建比赛名册:

class Game(models.Model):
DateTime = models.DateTimeField()
opponent = models.CharField(max_length = 50, default="TBD")
players = models.ManyToManyField(Player, limit_choices_to={'id__in': Player.objects.filter(active='True')},blank=True)
到目前为止还不错。但是我还想在球员人数众多的字段中添加一些自定义验证,以便比赛名册符合联盟关于最小和最大名册大小的规则,以及其他内容

当我调用下面的clean()方法时,返回了一个ValueError('游戏'实例需要有一个主键值,才能使用多对多关系。):

def清洁(自,*args,**kwargs):
从django.core.exceptions导入ValidationError
#self.save()
players\u count=self.players.count()
#不允许名单上的球员少于9人
如果玩家数量小于9:
raise ValidationError('名册必须至少有9名玩家')
如果我插入注释掉的self.save,验证器会工作,但最终会有多个游戏条目


这是我的第一个非教程Django项目,我对它还不熟悉,所以我猜有一个相当简单的答案。我浏览了这里和Django文档,尝试了一些看起来很有希望的东西,但没有任何效果。

在clean方法中,您只想进行验证检查。所以,从一开始,挽救它是个坏主意。因此,从form对象可以获得players字段的值并对其运行验证。当重写form clean方法时,请记住返回已清理的_数据

def clean(self, *args, **kwargs):
    from django.core.exceptions import ValidationError
    players = self.cleaned_data.get('players', [])
    players_count = len(players):
    # Don't allow rosters with fewer than 9 players
    if players_count < 9:
        raise ValidationError('Rosters must have at least 9 players')
    return self.cleaned_data
def清洁(自,*args,**kwargs):
从django.core.exceptions导入ValidationError
players=self.cleanned_data.get('players',[])
玩家数量=len(玩家):
#不允许名单上的球员少于9人
如果玩家数量小于9:
raise ValidationError('名册必须至少有9名玩家')
返回自清理的_数据

恐怕这对我不起作用。获取了一个属性错误:'Game'对象没有属性'cleaned_data'。这个答案基本上是可行的,但我并没有创建一个新的表单类来处理它。
def clean(self, *args, **kwargs):
    from django.core.exceptions import ValidationError
    players = self.cleaned_data.get('players', [])
    players_count = len(players):
    # Don't allow rosters with fewer than 9 players
    if players_count < 9:
        raise ValidationError('Rosters must have at least 9 players')
    return self.cleaned_data