I'm fairly new to Django. I've a problem ordering queryset by a related model field. I've already seen the similar questions on stackoverflow but I'm stuck with this silly error,
I would like have all the records from "MktIn" ordered by "nome" field from "Squadre" model The relation is one (Squadre) toMany (MktIn). Following the Django docs I've used the double underscore notation "squadre__nome" but i get this error :
p.s. all except the order_by clause works fine
Could you please help me??
>>> 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
in 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)
in 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()})
You should use,
It's your field name, not the model name.