django 1.7 order by related field 错误

django 1.7 order by related field error

我是 Django 的新手。我在按相关模型字段排序查询集时遇到问题。 我已经在 Whosebug 上看到过类似的问题,但我一直被这个愚蠢的错误所困扰,

我希望 "MktIn" 中的所有记录按 "Squadre" 模型中的 "nome" 字段排序 关系是一个 (Squadre) 到许多 (MktIn)。 在 Django 文档之后,我使用了双下划线符号 "squadre__nome" 但我收到此错误:

p.s。除了 order_by 子句之外的所有子句都可以正常工作

你能帮帮我吗??

>>> tutteIn = MktIn.objects.all().order_by('squadre__nome', 'ruolo', '-ingaggio')                                                                   
>>> print (tutteIn.query)
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/home/bestfoot/.virtualenvs/django17/lib/python3.4/site-packages/django/db/models/sql/query.py", line 196, in __str__
    sql, params = self.sql_with_params()
  File "/home/bestfoot/.virtualenvs/django17/lib/python3.4/site-packages/django/db/models/sql/query.py", line 204, in sql_with_params
    return self.get_compiler(DEFAULT_DB_ALIAS).as_sql()
  File "/home/bestfoot/.virtualenvs/django17/lib/python3.4/site-packages/django/db/models/sql/compiler.py", line 101, in as_sql
    ordering, o_params, ordering_group_by = self.get_ordering()
  File "/home/bestfoot/.virtualenvs/django17/lib/python3.4/site-packages/django/db/models/sql/compiler.py", line 429, in get_ordering
    self.query.get_meta(), default_order=asc):
  File "/home/bestfoot/.virtualenvs/django17/lib/python3.4/site-packages/django/db/models/sql/compiler.py", line 465, in find_ordering_name
    field, targets, alias, joins, path, opts = self._setup_joins(pieces, opts, alias)
  File "/home/bestfoot/.virtualenvs/django17/lib/python3.4/site-packages/django/db/models/sql/compiler.py", line 498, in _setup_joins
    pieces, opts, alias)
  File "/home/bestfoot/.virtualenvs/django17/lib/python3.4/site-packages/django/db/models/sql/query.py", line 1419, in setup_joins
    names, opts, allow_many, fail_on_missing=True)
  File "/home/bestfoot/.virtualenvs/django17/lib/python3.4/site-packages/django/db/models/sql/query.py", line 1383, in names_to_path
    self.raise_field_error(opts, name)
  File "/home/bestfoot/.virtualenvs/django17/lib/python3.4/site-packages/django/db/models/sql/query.py", line 1389, in raise_field_error
    "Choices are: %s" % (name, ", ".join(available)))
django.core.exceptions.FieldError: Cannot resolve keyword 'squadre' into field. Choices are: author, author_id, caratteristica_1, caratteristica_2, 
con_esperienza, created_date, extra_info_1, extra_info_2, id, id_squadra, id_squadra_id, incroci, ingaggio, mod_date, note, parametro_zero, piede, r
anking_in, ruolo, scadenza, sospesa, tipo_in, valore

在models.py中:

from django.db import models
from django.utils import timezone

class Squadre(models.Model):
    EL_SERIE = (('','Scegli'), ('A','A'), ('B','B'), ('C','C'), ('D','D'),)
    nome = models.CharField(max_length=100, unique=True)
    paese = models.CharField(max_length=50, blank=True, null=True)
    serie = models.CharField(choices=EL_SERIE, max_length=1, blank=True, null=True)
    indirizzo = models.CharField(max_length=200, blank=True, null=True)
    tel_sede = models.CharField(max_length=20, blank=True, null=True)
    fax_sede = models.CharField(max_length=20, blank=True, null=True)
    email_sede = models.EmailField(blank=True, null=True)    
    note = models.TextField(blank=True, null=True)
    author = models.ForeignKey('auth.User')
    created_date = models.DateTimeField(
            default=timezone.now)
    mod_date = models.DateTimeField(
            blank=True, null=True)

    def upd_date(self):
        self.mod_date = timezone.now()
        self.save()

    def __str__(self):
        return self.nome

class MktIn(models.Model):
    EL_RUOLI = (('','Scegli'), ('PO','Portiere'), ('TD','Terzino destro'), ('TS','Terzino sinistro'), ('DC','Difensore centrale'), ('CC','Centrocampista centrale'), ('CI','Interno di centrocampo'), ('CL','Centrocampista laterale'), ('TR','Trequartista'), ('AL','Ala'), ('AT','Attacante'),)
    EL_CARATTERISTICHE = (('','Scegli'),(1,'Bomber'),(2,'Box to box'),(3,'Di manovra'),(4,'Di spinta'),(5,'Difensivo'),(6,'Gioco aereo'),(7,'Normo dotato'),(8,'Play'),(9,'Rapido'),(10,'Strutturato'),(11,'Tecnico'),(12,'Veloce'),)
    EL_PIEDI = (('','Scegli'), ('DX','Destro'), ('SX','Sinistro'),)
    EL_TIPO_IN = (('','Scegli'),(1,'Definitivo'),(2,'Prestito'),(3,'Definitivo/Prestito'))
    EL_RANKING = (('','Scegli'),(200,'A'),(175,'AB'),(150,'B'),(125,'BC'),(100,'C'),(50, 'SG'))
    EL_SCAD = [[i, str(i)] for i in range(2015,2040)]
    id_squadra = models.ForeignKey(Squadre)
    ranking_in = models.SmallIntegerField(choices=EL_RANKING)
    ruolo = models.CharField(choices=EL_RUOLI, max_length=2)
    caratteristica_1 = models.SmallIntegerField(choices=EL_CARATTERISTICHE,blank=True,null=True)
    caratteristica_2 = models.SmallIntegerField(choices=EL_CARATTERISTICHE,blank=True,null=True)
    con_esperienza = models.NullBooleanField()
    piede = models.CharField(choices=EL_PIEDI, max_length=10,blank=True,null=True)
    tipo_in = models.SmallIntegerField(choices=EL_TIPO_IN,blank=True,null=True)
    valore = models.PositiveIntegerField()
    ingaggio = models.PositiveIntegerField()
    scadenza = models.SmallIntegerField(choices=EL_SCAD,blank=True,null=True)
    parametro_zero = models.NullBooleanField()
    extra_info_1 = models.CharField(max_length=100, blank=True, null=True)
    extra_info_2 = models.CharField(max_length=100, blank=True, null=True)
    note = models.TextField(blank=True, null=True)
    author = models.ForeignKey('auth.User')
    created_date = models.DateTimeField(
            default=timezone.now)
    mod_date = models.DateTimeField(
            blank=True, null=True)
    sospesa = models.BooleanField(default=False)

    def upd_date(self):
        self.mod_date = timezone.now()
        self.save()

    def __str__(self):
         return "%s %s" % (self.id_squadra, self.ruolo)

在views.py中:

from .models import Squadre, MktIn, MktOut, Incroci

def lista_in(request):
    tutteIn = MktIn.objects.all().order_by('squadre__nome', 'ruolo', '-ingaggio')
    return render(request, 'market/lista_in.html', {'tutteIn':tutteIn, 'full_path': request.get_full_path()})

你应该使用,

tutteIn = MktIn.objects.all().order_by('id_squadra__nome', 'ruolo', '-ingaggio')    

这是您的字段名称,而不是模型名称。