Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.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_Model - Fatal编程技术网

是否可以在Django中将模型字段分组?

是否可以在Django中将模型字段分组?,django,model,Django,Model,在日历应用程序上工作,并希望每个事件模型实例都填写一个{allday | start,end}字段。也就是说,要么输入全天字段,要么输入开始+结束字段,但不能同时输入两者 如何在管理应用程序中对此进行建模并使其正常工作?我希望需要一个组。使用所有3个字段创建您的模型,并覆盖clean方法(验证模型时调用)以检查您的条件: def clean(self): if not self.allday: # allday not present if not self.start

在日历应用程序上工作,并希望每个事件模型实例都填写一个{allday | start,end}字段。也就是说,要么输入全天字段,要么输入开始+结束字段,但不能同时输入两者


如何在管理应用程序中对此进行建模并使其正常工作?我希望需要一个组。

使用所有3个字段创建您的模型,并覆盖clean方法(验证模型时调用)以检查您的条件:

def clean(self):
    if not self.allday: # allday not present
        if not self.start or not self.end: # start and/or end not present
            raise ValidationError('error message...')
    else:
        if self.start or self.end:     # allday present but also start and/or end
            raise ValidationError('error message...')

有关清理的详细信息:

使用所有3个字段创建模型,并覆盖清理方法(在验证模型时调用)以检查您的条件:

def clean(self):
    if not self.allday: # allday not present
        if not self.start or not self.end: # start and/or end not present
            raise ValidationError('error message...')
    else:
        if self.start or self.end:     # allday present but also start and/or end
            raise ValidationError('error message...')
有关清洁的更多信息: