Reputation: 261
I tried querying the Db with this bit of code:
Makesite.objects.values_list('ref_id', flat =True)
and it returned [1,2,None]. Which I found to be moderately confusing. I assumed that python saved the instances by their own names and not numbers that it just assigns to them. Any help with the code or an explanation to why python saves them as numbers and not their names would be awesome thanks.
In models.py
class Makesite(models.Model):
sitename = models.CharField(max_length=100, unique = True)
siteinfo = models.ManyToManyField(Siteinfo)
ref_id = models.ManyToManyField(RefID)
report = models.ManyToManyField(Report)
Upvotes: 0
Views: 46
Reputation: 31951
Django doesn't save m2m by their "names". It uses their primary keys (in your case - integers). You can check it, by viewing your DB tables.
When you just use something like makesite.sitename
Django makes anither query to fetch sitename instance.
But when you use values_list
you don't want extra queries or joins, that's why Django returns data from a single table. And all that it can get form there is a primary key.
If you want to optimize your related queries take a look at select_related / prefetch_related methods or use caching.
Upvotes: 1