只是偶然发现了一些人的代码
他有这样的模型
class Country(models.Model):
name = models.CharField(max_length=100)
class TourDate(models.Model):
artist = models.ForeignKey("Artist")
date = models.DateField()
country = models.ForeignKey("Country")
并像这样查询
ireland = Country.objects.get(name="Ireland")
artists = Artist.objects.all().extra(select = {
"tourdate_count" : """
SELECT COUNT(*)
FROM sandbox_tourdate
JOIN sandbox_country on sandbox_tourdate.country_id = sandbox_country.id
WHERE sandbox_tourdate.artist_id = sandbox_artist.id
AND sandbox_tourdate.country_id = %d """ % ireland.pk,
}).order_by("-tourdate_count",)
我的问题是,为什么他有像sandbox_tourdate这样的下划线,但它不在模型字段中,是不是像某种伪字段一样自动创建的?
发布于 2013-07-18 23:10:05
sandbox_tourdate
不是字段的名称,而是表的名称。Django的命名约定是使用appname_modelname
作为表名,尽管这可以被覆盖。在这种情况下,我猜这个应用程序被称为‘沙盒’。
我真的不知道那个人为什么要使用原始查询,这在Django的ORM语法中很容易表达。
https://stackoverflow.com/questions/17726829
复制相似问题