sort by alphabet

26 views
Skip to first unread message

divya murugulla

unread,
Apr 11, 2022, 3:47:31 PM4/11/22
to Django users
Please help me sort the queryset by name alphabetical order.

 plant = forms.ModelChoiceField(label='Destinations Name',queryset=GlobePlant.objects.all(),empty_label='Select the Point',required=True)  

Thanks & Regards,
Divya     

Antonis Christofides

unread,
Apr 11, 2022, 4:30:50 PM4/11/22
to django...@googlegroups.com

Hi,

You need to replace

queryset=GlobePlant.objects.all()

with

queryset=GlobePlant.objects.all().order_by(...)

I can't tell you what arguments to give "order_by()" without seeing your GlobePlant model, but if you read Django's documentation for order_by you should be able to determine that.

Regards,

Antonis

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/56143c58-7525-4cab-959b-acf79b50a7dbn%40googlegroups.com.

divya murugulla

unread,
Apr 11, 2022, 5:03:24 PM4/11/22
to Django users
latamSupplyPoints = [ (e.globe_code_id, e.globe_code.plant_name) for e in CommodityLocationXref.objects.select_related('globe_code','cmdty_loc').filter(cmdty_loc_id__in=LatamLocationIds) ] 

how to sort these values

Antonis Christofides

unread,
Apr 11, 2022, 5:14:14 PM4/11/22
to django...@googlegroups.com

Same thing, use CommodityLocatinoXref.objects.select_related(...).filter(...).order_by(...)

divya murugulla

unread,
Apr 11, 2022, 5:15:13 PM4/11/22
to Django users
that is not working since my sort function is of different table

Antonis Christofides

unread,
Apr 11, 2022, 6:20:18 PM4/11/22
to django...@googlegroups.com

Please read the documentation of order_by(). It tells you how you can do this.

Reply all
Reply to author
Forward
0 new messages