[英]Django: Make field required if other field entered
如果用戶在video_url字段中輸入一個值,我試圖使該字段成為video_thumbnail ,反之亦然。 但是,如果兩者都為空,則不需要。
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)
您可以覆蓋保存方法,
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.