Either/Or fields in Django models
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?
This isn't something you'd deal with in the model definition. You would handle it at the form level, by making both fields required=False but checking in the form's clean() method that one of them was supplied.