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?


You can use a ModelForm and override its clean method to get the behaviour you want. If you want to use the admin, you can add the custom behaviour with a ModelAdmin

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.

Need Your Help

The YUI maze: How do I get my CSS minified?

css documentation yui minify

What steps should I follow to be able to minify CSS from the command line (Windows or Solaris)?

titleLabel.text vs currentTitle in Swift

swift uibutton

I am trying to make a simple calculator with Swift. I want to get the "text" on the buttons I created. The instructor in the tutorial is using a property: