按外键命令Django查询结果

cha*_*sie 20 django django-models django-queryset

我有一个模型,设置如下:

class Log(models.Model):
    name = models.ForeignKey(User)
    date = models.DateField()
    time = models.TimeField()
Run Code Online (Sandbox Code Playgroud)

我知道这不起作用,但有没有其他方法我可以运行这样的查询:

Logs.objects.filter(date=someDate).order_by('name__last_name')
Run Code Online (Sandbox Code Playgroud)

我只需要将最终结果按照与之QuerySet相关的用户的姓氏排序ForeignKey.

我真的很想知道这个.任何事情都会有所帮助:一些我没有看过的方法,一个真正的原始SQL查询,甚至只是一个普遍的想法,我将不胜感激!

DTi*_*ing 20

您输入的查询看起来有效.

按文档查看订单here.

它不适合你吗?

例如(格式化以便于阅读):

    >>> units = Unit.objects.filter(color='red').order_by('location__label')
    >>> print units.query
    SELECT `samples_unit`.`id`, `samples_unit`.`location_id`, `samples_unit`.`color` 
      FROM `samples_unit` 
INNER JOIN `storages_container` 
        ON (`samples_unit`.`location_id` = `storages_container`.`id`) 
     WHERE `samples_unit`.`color` = red  
  ORDER BY `storages_container`.`label` ASC
Run Code Online (Sandbox Code Playgroud)