Does anybody have an ideia how to order by a foreign key? I spent a
lot of
time for searching any solution. Unfortunatelly without success.
### CODE ###
class Person(models.Model):
nameLast = models.CharField ('Nachname', maxlength = 31)
nameFirst = models.CharField ('Vorname', maxlength = 31)
address = models.CharField ('Adresse', maxlength = 63)
def __str__(self):
return "%s, %s " % (self.nameLast, self.nameFirst)
class Admin:
list_display = ('nameLast', 'nameFirst', 'address')
fields = (
('Personalien', {'fields': ('nameLast',
'nameFirst', 'address')})
)
class Meta:
ordering = ('nameLast', 'nameFirst', 'location')
verbose_name = "Person"
verbose_name_plural = "Personen"
class Karateka(models.Model):
person = models.ForeignKey (Person, verbose_name = 'Person',
core = True)
bsc = models.BooleanField ('BSC')
comment = models.TextField ('Bemerkung', blank = True, null =
True)
def __str__(self):
return "%s" % (self.person)
class Admin:
list_display = ('person')
fields = (
(None, {'fields': ('person')})
)
def name_last(self):
return "%s" % (self.person.nameLast)
class Meta:
ordering = ('person')
verbose_name = 'Karateka'
verbose_name_plural = 'Karatekas'
### END CODE ###
Ordering by related fields is a bit crufty at the moment and it doesn't
always work (e.g. ticket #2076). The general idea is that you have to
use the database table name(!) followed by a dot followed by the target
model field name (not the column name). This is actually documented at
http://www.djangoproject.com/documentation/db-api/#order-by-fields but
you have to read it carefully.
The good news is that this will shortly change so that you will be able
to write person__nameLast in your case, just like you do for filters.
That code is currently in the queryset-refactor branch and will be
merged with trunk as soon as a few more slightly tricky problems with
queries are ironed out.
By way of example and as a sneak preview, [1] and [2] (in the Meta
class) show the new syntax (that's a new test file on that branch).
Regards,
Malcolm
--
Remember that you are unique. Just like everyone else.
http://www.pointy-stick.com/blog/
On Oct 19, 3:11 pm, Malcolm Tredinnick <malc...@pointy-stick.com>
wrote:
> On Fri, 2007-10-19 at 06:54 -0700, äL wrote:
> > I would like to order a list in a view by foreign key. If I try
> >orderinghow in the
> > code below my list is ordered by 'person'. And this means that the
> > list
> > ist ordered by the ID of the table Person. But I need a list ordered
> > by 'nameLast'.
> > Thus I changedordering'person' into 'person.nameLast'. But then I
> > got an error
> > "Column not found".
>
> > Does anybody have an ideia how to order by a foreign key? I spent a
> > lot of
> > time for searching any solution. Unfortunatelly without success.
>
> Orderingby related fields is a bit crufty at the moment and it doesn't
> always work (e.g. ticket #2076). The general idea is that you have to
> use the database table name(!) followed by a dot followed by the target
> model field name (not the column name). This is actually documented athttp://www.djangoproject.com/documentation/db-api/#order-by-fieldsbut
> you have to read it carefully.
>
> The good news is that this will shortly change so that you will be able
> to write person__nameLast in your case, just like you do for filters.
> That code is currently in the queryset-refactor branch and will be
> merged with trunk as soon as a few more slightly tricky problems with
> queries are ironed out.
>
> By way of example and as a sneak preview, [1] and [2] (in the Meta
> class) show the new syntax (that's a new test file on that branch).
>
> [1]http://code.djangoproject.com/browser/django/branches/queryset-refact...
>
> [2]http://code.djangoproject.com/browser/django/branches/queryset-refact...
>
> Regards,
> Malcolm
>
> --
> Remember that you are unique. Just like everyone else.http://www.pointy-stick.com/blog/
Hello Malcolm,
could you make a rough estimate how long it will take to merge the
queryset-refactor branch trunk?
Regards
Bernd
My models:
class Member(models.Model):
# first_name, last_name, etc.
class Meta:
ordering = ['first_name', 'last_name']
class Team(model.Model):
foreman = model.ForeignKey(Member)
operator = model.ForeignKey(Member)
# and more ForeignKey(Member) fields
class Meta:
ordering = ['foreman',]
In the admin interface this orders the Teams by the first_name,
last_name of the foreman, which is what I want. But in my view, when
I try to get a list of Teams using:
all_teams = Team.objects.all()
the result isn't sorted by the foreman's [first_name, last_name]. I
don't know what it's sorted by, it's not id either.
I also tried:
all_teams = Teams.objects.all().order_by('foreman') # returns the
same as above, again not sorted.
[1] all_teams =
Teams.objects.all().order_by('appname_member.first_name') # returns
error on eval, unknown column 'appname_member.first_name'.
The order_by(tablename.field) worked in another model with only one
reference to the foreign key (so, thanks for that tip, I missed it in
the docs.) I didn't expect [1] to work, but don't know how to specify
that I want the sorting based on the foreman's first_name, last_name.
Any suggestions? Is this possible using order_by()? I can do it by
hand if not, but it'd be cleaner and simpler if I don't have to.
Many thanks.
Michael
On Oct 19, 7:11 am, Malcolm Tredinnick <malc...@pointy-stick.com>
wrote:
> Orderingby related fields is a bit crufty at the moment and it doesn't
> always work (e.g. ticket #2076). The general idea is that you have to
> use the database table name(!) followed by a dot followed by the target
> model field name (not the column name). This is actually documented athttp://www.djangoproject.com/documentation/db-api/#order-by-fieldsbut
> you have to read it carefully.
>
> The good news is that this will shortly change so that you will be able
> to write person__nameLast in your case, just like you do for filters.
> That code is currently in the queryset-refactor branch and will be
> merged with trunk as soon as a few more slightly tricky problems with
> queries are ironed out.
>
> By way of example and as a sneak preview, [1] and [2] (in the Meta
> class) show the new syntax (that's a new test file on that branch).
>
> [1]http://code.djangoproject.com/browser/django/branches/queryset-refact...
>
> [2]http://code.djangoproject.com/browser/django/branches/queryset-refact...
Try inspecting the SQL that Django generates for this statement (if you
don't know how, see the FAQ, but db.connection.queries is the place to
look) to see what's being generated.
You may well be totally screwed here, in which case I'm not going to
worry about it too much, since this feature is effectively broken in
trunk. When it works, that's fine. But it's not robust and is very
unpredictable. That's why I've rewritten it in the branch. So if the SQL
generated is broken, I can't really offer much hope except to say "wait
a little longer" until we get the rewrite finished. I'm targetting end
of the month (Django sprint) for a rough completion date.
Malcolm
--
On the other hand, you have different fingers.
http://www.pointy-stick.com/blog/