[Answer]-Overriding form validation for empty "unique" field for Django Crispy Forms

1👍

You can have multiple students with anonymous_id=None, the problem is that your form is saving anonymous_id=''. If you use a ForeignKey instead of a CharField, then Django will save None instead of empty string for you, and validate that the ids link to an object that exists.

I would be surprised if your form behaved differently with and without crispy forms. The problem is that you now have an empty string in the database, so you can’t add another.

I would suggest using a ForeignKey instead of a CharField, or checking the ids and changing empty strings to None before saving your form to the db. You will also have to go through your existing students and convert any empty strings to None.

Leave a comment