2015-06-11 80 views
1

我對Django相當陌生。我有一個問題,通過相關模型字段排序查詢集。 我已經看到計算器上的類似的問題,但我堅持了這個愚蠢的錯誤,django 1.7相關字段錯誤訂單

我想擁有的所有記錄從「MktIn」,從「Squadre」模式 的關係由「諾姆」字段排序是一個(Squadre)toMany(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()}) 
+0

嘗試'.order_by('id_squadra__nome ...' – itzMEonTV

+0

Thanks itzmeontv,It works !!! 我確定必須使用relatedmodel__fied語法,但我錯了 – jonky

回答

0

你應該使用,

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

這是你的字段名,而不是模型名稱。