Python – What is the equivalent of assertDatabaseHas in Django?

What is the equivalent of assertDatabaseHas in Django?… here is a solution to the problem.

What is the equivalent of assertDatabaseHas in Django?

Background from Laravel to learn Django. Forgive me if my question is childish.

response = self.client.delete(reverse('details'), kwargs={'pk':pk}, format="json")

self.assertEqual(response.status_code, status. HTTP_204_NO_CONTENT)

The above test passed, I want to go a step further and try to check if the database really has this item, of course I can query the database and try to match the count.
But in Laravel I use these methods to check for the presence/absence of a record.

assertDatabaseHas('users', ['id' => 10]); asserts true if record is present in users table.

assertDatabaseMissing('users', ['id' => 10]);  asserts true if record is not present in users table

Is there something similar in Django?

Solution

There are no specific assertions about this.

You might be able to use a generic assertTrue and exists filter:

self.assertTrue(User.objects.filter(id=10).exists())

(Normally you would use get() for id queries, but this does not allow exists() and you must catch the DoesNotExist exception if not found.) )

Related Problems and Solutions