1
當我到達的對象,我怎麼能優先考慮字段的順序在過濾器
objs = Page.objects.get(slug="some-slug.html", web_id=1)
生成的SQL查詢
select * from cms_page WHERE ("cms_page"."web_id" = '1' AND "cms_page"."slug" = '''some-slug''')
我想更改SQL,以
select * from cms_page WHERE ("cms_page"."slug" = '''some-slug''' and "cms_page"."web_id" = '1')
第一個slu field字段,因爲slug字段是一個索引(Exist index with this field)
class Page(models.Model):
title = models.CharField(max_length=128)
body = models.TextField(blank=True)
slug = models.CharField(max_length=200, db_index=True)
web = models.ForeignKey(Web, editable=False)
def __unicode__(self):
return self.title
def save(self, *args, **kwargs):
if not self.slug:
self.slug = "{0}{1}".format(slugify(self.title),".html")
super(Page,self).save(*args,**kwargs)
class Meta:
unique_together = ("web", "slug")
index_together = ["web", "slug"]
數據庫優化器無關緊要。 – Todor
爲什麼不,slug字段是一個索引 – erajuan