[英]Django: Make field required if other field entered
I'm trying to make the field video_thumbnail become required if the user enter a value into the field video_url and vice versa. 如果用户在video_url字段中输入一个值,我试图使该字段成为video_thumbnail ,反之亦然。 But if both are empty then they are not required.
但是,如果两者都为空,则不需要。
models.py models.py
class Post(TimeStampedModel):
video_url = models.CharField(max_length=255, blank=True)
video_thumbnail = ResizedImageField(upload_to=file_name, storage=upload_storage, max_length=255, blank=True, null=True)
You can override save method, 您可以覆盖保存方法,
class Post(TimeStampedModel):
video_url = models.CharField(max_length=255, blank=True)
video_thumbnail = ResizedImageField(upload_to=file_name, storage=upload_storage, max_length=255, blank=True, null=True)
def save(self, *args, **kwargs):
if not self.video_url and self.video_thumbnail == None:
raise ValueError("The value isn't allowed")
super(Post, self).save(*args, **kwargs)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.