I am running a test on my new Django code. I have the following models:
class Places(models.Model):
name = models.CharField(max_length=20)
street = models.CharField(max_length=50)
number = models.IntegerField()
confirmed = models.BooleanField()
notes = models.TextField()
def __unicode__(self):
return self.address
class Meta:
db_table = "Places" # to prevent the prefixes from being added on
When I do this in the manage.py shell I get the correct response :
from my_app.models import Places
Places.objects.get(name='home').confirmed
>>> True
When this same code is performed in the my app under a unittest (manage.py test myapp) I get the error:
Places matching query does not exist django
When I tried with PDB I found that Django is reading the database as being empty (an empty set) and not as storing any data.
I also tried RAW SQL in the code and that failed as well, but worked in shell.
I checked with the database in sql itself and the data exists. I performed the equivalent SQL code and it returns True as well.
Is this a bug in Django?
Unit tests are always executed against a new empty database. Have you created the data within the setup step of your test, or provided a data fixture?