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