开发者

Better way to query this Django Model

开发者 https://www.devze.com 2023-03-28 13:33 出处:网络
I am new to Django. I have a model and now I need to query the model. The model is this: from django.db import models

I am new to Django. I have a model and now I need to query the model.

The model is this:

from django.db import models
import datetime

class Position(models.Model):
    website_position = models.IntegerField()
    category = models.ForeignKey('Category')
    directorio = models.ForeignKey('Directorio')

class Category(models.Model):
    n_category = models.CharField(max_length=100)
    category_position = models.IntegerField()
    date_inserted = models.DateTimeField(auto_now_add=True)
    date_last_update = models.DateTimeField(auto_now_add=True)

class Directorio(models.Model):
    website_name = models.CharField(max_length=200)
    website_url = models.URLField()
    activated = models.BooleanField()
    date_inserted = models.DateTimeField(auto_now_add=True)
    date_last_update = models.DateTimeField(auto_now=True)
    categories = models.ManyToManyField(Category, through=Position)

Now, I need to query the database in this way:

select dc.n_category, dp.website_position, dd.website_name, dd.website_url, dd.activated
from directorio_category as dc
join directorio_开发者_Python百科position as dp on dc.id = dp.category_id
join directorio_directorio as dd on dp.directorio_id = dd.id
where dd.activated = 'true'

Question: Should I use the Model Query API or should I use RAW SQL?

Best Regards,


You can do it with the model query api, but if you already have the raw sql written that's going to be more efficient in the long term if you have to scale to enormous heights.

Pro of raw SQL: More efficiently hit the database. Pro of query api: Non SQL django people will be able to maintian and extend your code in the future.

I've been interacting with databases via django's orm so long that I'm struggling to figure out what your query even means.

#I think this gets what you want
positions = Position.objects.filter(directorio__activated=True).order_by('category__n_catgory', 'website_position')
for position in positions:
    print position.category.n_category, position.website_position, position.directorio.website_name, position.website.website_url, position.website.activated

The key to migrating from SQL to django's ORM is starting to think in terms of the primary object(s) you want, then walking the relationships to get the related data. All data related to an object is available to you via object/dot notation, and thanks to the django ORM's laziness, most of it isn't retrieved until you ask for it.

The above code gets every position by category, then walks the relationships to get the other data. Another version of the code might get every active website then show its categories and positions:

websites = Directorio.objects.filter(activated=True).order_by('website_name')
for website in websites:
    print website.website_name, website.website_url, website.activated
    for position in website.position_set.all().order_by('category__n_category'):
        print " -- ", position.category.n_category, position.website_position
0

精彩评论

暂无评论...
验证码 换一张
取 消