site/posts/Silly_django_confusion.mdwn

44 lines
1.2 KiB
Text
Raw Permalink Normal View History

I'm setting up a testing environment for work, using fixtures to save
and load test data and I got a little stumped by something I ran into.
I had exported one of the database tables we use to a json file that I
was going to import into a fresh new database to test with. When I
imported it everything seemed fine, except when looking at the actual
page. So this is what I found:
2012-11-04 12:35:00 +01:00
[[!format sql """
SELECT * FROM app_table;
2012-11-04 19:13:22 +01:00
=> 3 rows of data
2012-11-04 12:35:00 +01:00
"""]]
2012-11-04 19:13:22 +01:00
[[!format py """
from app.models import Table
Table.objects.count()
2012-11-04 19:13:22 +01:00
=> 3
2012-11-04 19:13:22 +01:00
Table.objects.all()
=> []
"""]]
This is weird. So I looked at the `django.db.connection.queries`
property and I realized that it was doing a join since the model
subclasses another:
2012-11-04 19:13:22 +01:00
[[!format py """
from django.db import models
2012-11-04 19:13:22 +01:00
from app.models import SuperTable
2012-11-04 19:13:22 +01:00
class Table(SuperTable):...
"""]]
Which, of course, means that in order to get the complete `Table`
instance, the related `SuperTable` instance is also required, but in
order to do a `COUNT` of `app_table` it isn't necessary. And that's
where the inconsistency came from, now that I've also copied the
contents of `SuperTable` everything is fine again.
[[!meta date="2012-04-24 15:51:00"]]
[[!tag python django coding]]