I wanted to check that object exists in the db, and if it does not exist, to add it to the database.
I tried:
if not MyModel.objects().get(surname='foo'):
management.call_command('loaddata', 'Bootstrap_data', verbosity=0)#adds this object from fixtures
But I get a query error from db(sqlite3). How can this way of object validation be resolved?
The error is:
DoesNotExist at /
MyModel matching query does not exist.
It is because there is no object with this surname in the db.
Typically you’d use get_or_create.
You shouldn’t really be using
management.call_commandfor something like this.That said, if I’ve misunderstood and you have a good reason, try this:
OR