Re: Ordering by foreign key, class Meta

2007-11-13 Thread Malcolm Tredinnick


On Wed, 2007-11-14 at 00:00 +, Michael wrote:
> Thanks Malcolm, this helped me on one problem I had.  But, I've
> another similar problem but in my case, I've multiple foreign keys, so
> I'm not sure what to do about the database table name part.
> 
> 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.

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/


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Ordering by foreign key, class Meta

2007-11-13 Thread Michael

Thanks Malcolm, this helped me on one problem I had.  But, I've
another similar problem but in my case, I've multiple foreign keys, so
I'm not sure what to do about the database table name part.

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 <[EMAIL PROTECTED]>
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...
>
> Regards,
> Malcolm
>
> --
> Remember that you are unique. Just like everyone 
> else.http://www.pointy-stick.com/blog/


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Ordering by foreign key, class Meta

2007-11-04 Thread Brot



On Oct 19, 3:11 pm, Malcolm Tredinnick <[EMAIL PROTECTED]>
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


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Ordering by foreign key, class Meta

2007-10-19 Thread Malcolm Tredinnick

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
> ordering how 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 changed ordering '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.

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).

[1]
http://code.djangoproject.com/browser/django/branches/queryset-refactor/tests/regressiontests/queries/models.py#L290

[2]
http://code.djangoproject.com/browser/django/branches/queryset-refactor/tests/regressiontests/queries/models.py#L62

Regards,
Malcolm

-- 
Remember that you are unique. Just like everyone else. 
http://www.pointy-stick.com/blog/


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Ordering by foreign key, class Meta

2007-10-19 Thread äL

I would like to order a list in a view by foreign key. If I try
ordering how 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 changed ordering '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.


### 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 ###


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---