[Answered ]-How can I deal with broken contraints when unit testing in Django?

2👍

You can assign the foreign key id directly and then save the model:

m = MyModelA()
m.my_foreign_key_id = n
m.save()

where n is a non-existent id. That will fail with matching query does not exist if you try to access the foreign key.

Leave a comment