django 1.7按相关字段错误排序

时间:2015-06-11 13:43:25

标签: python django django-queryset

我对Django很新。我通过相关的模型字段排序查询集的问题。 我已经在stackoverflow上看到了类似的问题,但我仍然坚持这个愚蠢的错误,

我想拥有来自" MktIn"的所有记录。由" nome"命令来自" Squadre"模型 关系是一个(Squadre)到Many(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()})

1 个答案:

答案 0 :(得分:0)

你应该使用,

Dim runningTotals As System.Collections.Hashtable


Public Function AddRunningTotalValue(ByVal dateStr As Object, ByVal amount As Object)
        If (runningTotals Is Nothing) Then
            runningTotals = New System.Collections.Hashtable
        End If
        If (Not runningTotals.Contains(dateStr)) Then
            runningTotals.Add(dateStr, amount)
       End If
       AddRunningTotalValue = amount
End Function

Public Function GetRunningTotalValue(ByVal dateStr)
    If (Not runningTotals.Contains(dateStr)) Then
        GetRunningTotalValue = 0
    End If        
        If (runningTotals.Contains(dateStr)) Then
            For Each dateStrPair As System.Collections.DictionaryEntry In runningTotals
                If dateStrPair.Key.ToString() = dateStr Then
                GetRunningTotalValue = dateStrPair.Value
            End If
        Next
        End If
End Function

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

相关问题