Thanks, Christian.

After trying your attempts, I solved the problem by creating an
additional "order" field in the ForeignKey. So in the referenced model
I had to do:

class Meta:
    ordering = ['fkfield__order']

That's not so elegant as I wished it to be, but it's not that ugly too, IMO.

Vinicius

2011/7/28 christian.posta <christian.po...@gmail.com>:
> Is this something that can work for you?
>
> https://docs.djangoproject.com/en/1.3/ref/models/options/#order-with-respect-to
>
> If not, then the only thing i can do is recommend you review the
> source code for how the ordering happens in the admin module for a
> model.
>
> The main spots to look at would be the get_ordering() method of the
> ChangeList class (django/contrib/admin/views/main.py) and the
> get_query_set() method of the same class. The ordering will definitely
> change, regardless of the queryset you override in the ModelAdmin
> class, but those two methods should give some clue as to what ordering
> options are available to you (default is descending by ID). you may
> wish to look closer than i did at the first line in the get_ordering()
> method which goes like this:
>        lookup_opts, params = self.lookup_opts, self.params
> there might be some additional ordering that can be accomplished with
> those variables.
>
> good luck, let me know if you figure it out
>
> On Jul 28, 6:07 am, Vinicius Massuchetto
> <viniciusmassuche...@gmail.com> wrote:
>> I got a model with a ForeignKey field, and I want the default listing
>> of this model to be ordered by this field.
>>
>> I get the correct `qs.query` SQL statement, but for some reason the
>> admin listing is not following it. This is what I've done so far:
>>
>>     class SomeAdminModel(admin.ModelAdmin):
>>         # ... fields
>>
>>         def queryset(self, request):
>>             qs = super(SomeAdminModel, self).queryset(request)
>>             fk_list = [1,2,7,3,5,6,4]
>>             ordering = 'FIELD(`fk_field_id`, %s)' % ','.join(str(id)
>> for id in fk_list)
>>             qs = qs.filter(fk_field__in = fk_list).extra(
>>                 select = {'ordering': ordering},
>>                 order_by = ('ordering',)
>>             )
>>             return qs
>>
>> What can be preventing this from behave as desired? Do I need to
>> override anything else?
>>
>> Thanks in advance.
>> --
>> Vinicius Massuchettohttp://vinicius.soylocoporti.org.br
>
> --
> 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 
> django-users+unsubscr...@googlegroups.com.
> For more options, visit this group at 
> http://groups.google.com/group/django-users?hl=en.
>
>



-- 
Vinicius Massuchetto
http://vinicius.soylocoporti.org.br

-- 
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 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to