I was building a django model that has two fields, only one of them is required.
class course_data(models.Model):
link = models.CharField(max_length = 500, null = True, blank = True)
uploaded_file = models.FileField(upload_to='course_files', null = True, blank = True)
name = models.CharField(max_length = 200, null = True, blank = True)
description = models.TextField(null = True, blank = True)
applies_to = models.ManyToManyField('course')
I want the entry to be valid only if the “link” field is provided or if the ‘uploaded_file’ field is provided or both. I can’t make both parameters optional since they can be both left blank. Any suggestions?
You can use a ModelForm and override its
cleanmethod to get the behaviour you want. If you want to use the admin, you can add the custom behaviour with a ModelAdmin