I just had the same question, and the meta 'ordering' did the trick.
Thanks Reed and Tim!

On Nov 30 2009, 8:22 am, rc <reedcr...@gmail.com> wrote:
> Tim,
>
> The ordering meta option on my model did the trick. Thanks for the
> solution.
>
> Reed
>
> On Nov 25, 1:50 pm, Tim Valenta <tonightslasts...@gmail.com> wrote:
>
>
>
> > Well, you've got a couple of options.  One is easiest and most
> > straightforward, but could impact performance.  The second is a little
> > bit more work, but would limit the ordering to only taking effect on
> > that one form.
>
> > The first way is to use the Meta option `ordering = ('fieldname',)`,
> > as described 
> > here:http://docs.djangoproject.com/en/dev/ref/models/options/#ordering
>
> > Bare in mind that this will passively affect ALL queries you do to
> > that model's objects, unless you specifically tell it not to order
> > during the query.  If you think that you'll *always* want that sort
> > order to take effect, then this is the preferred method of
> > accomplishing that.
>
> > The second way is to intercept the form's queryset powering the
> > widget.  If you're not using Form objects with your admin (ie, you're
> > just specifying 'fields' or 'fieldsets' on your ModelAdmin), then
> > you'll have to take a quick sidequest:
>
> > Create a class somewhere (preferably in your app's directory, in a
> > "forms.py" file or something obvious) like so:
>
> >     from django import forms
> >     from models import MyModel
> >     class MyModelForm(forms.ModelForm):
> >         class Meta:
> >             model = MyModel
> >         def __init__(self, *args, **kwargs):
> >             forms.ModelForm.__init__(self, *args, **kwargs)
> >             self.fields['myfieldname'].queryset =
> > MyModel.objects.order_by('custom_sort_field_name')
>
> > And then back in your ModelAdmin, add this attribute to the class:
>
> >     # ...
> >     from forms import MyModelForm
> >     form = MyModelForm
>
> > This will cause the admin to use *your* form instead of the default.
> > What I've done is created a form which simply hijacks the queryset on
> > 'myfieldname' (the field you're trying to sort), and alters it before
> > the widget has a chance to render itself.
>
> > Both methods are effective, but be sure to consider my initial
> > statement, about performance impact.  Pick the method that strikes the
> > balance in your mind between performance and practicality.
>
> > Hope that helps!
>
> > On Nov 25, 1:06 pm, rc <reedcr...@gmail.com> wrote:
>
> > > I have configured a modeladmin which uses thefilter_horizontal
> > > feature. I would like to be able to order left side of this feature by
> > > name instead of the default (which I believe is by id). Is this
> > > possible? I see how to use "ordering" in the display for change lists,
> > > but not sure how to apply that to the 'left' side data of the
> > >filter_horizontal.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@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