Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to order choices based on their name instead of their index?

This is my first post on stackoverflow, I'm going to try to follow the basic rules but be gentle with me.

I have to make a few changes (which appeared superficial at first glance) to a django app. The client wants all the dropdown menus of the forms in the admin part of the site to be sorted alphabetically. The problem is that some of these choices are based on static lists defined in the models.py files like this one :

STATE=(  
    (1, 'Full'),  
    (2, 'Damaged'),  
    (3, 'Partially damaged')  
)  

I tried to reorder the list like that :

STATE=(  
    (2, 'Damaged'),  
    (1, 'Full'),  
    (3, 'Partially damaged')  
)  

but it doesn't seem to make a difference (although I'm not really familiar with the way Python caches classes and views).

Reordering the indexes like that :

STATE=(  
    (1, 'Damaged'),  
    (2, 'Full'),  
    (3, 'Partially damaged')  
)  

works but would mean writing some huge and complicated SQL scripts to keep the old data consistent (since most of the lists are longer than this one and permutations would become more complex).

So my question is : Is there a way to sort these choices based on their name instead of the index ? Maybe in the definition of the models.IntegerField(..., choices=STATE) ?

Thank you all.

like image 820
paztek Avatar asked Mar 24 '11 10:03

paztek


People also ask

What is AutoField in Django?

According to documentation, An AutoField is an IntegerField that automatically increments according to available IDs. One usually won't need to use this directly because a primary key field will automatically be added to your model if you don't specify otherwise.

How do you use model choices in Django?

Django field choices are sequences consisting of iterables of exactly two items (e.g. [(A, B), (C, D) ...]) to use as choices for some field. If the choices are provided, they're enforced by model validation. The default form widget will be a select box with specified choices, not the standard text field.

What is null True in Django?

null = True. Means there is no constraint of database for the field to be filled, so you can have an object with null value for the filled that has this option. blank = True. Means there is no constraint of validation in django forms.

What are fields in Django?

Fields in Django are the data types to store a particular type of data. For example, to store an integer, IntegerField would be used. These fields have in-built validation for a particular data type, that is you can not store “abc” in an IntegerField.


2 Answers

changing the choices attribute of the form field to the sorted choices should fix your problem.

forms.py

class YourAdminForm(forms.Form):
    def __init__(self, *args, **kwargs):
        super(YourAdminForm, self).__init__(*args, **kwargs)
        sorted_choices = sorted(YourModel.STATE, key=lambda x: x[1])
        self.fields['field'].choices = sorted_choices

and:

from django.contrib import admin
from models import Your
from forms import YourAdminForm

class YourAdmin(admin.ModelAdmin):
    form = YourAdminForm

admin.site.register(Your, YourAdmin)
like image 193
dting Avatar answered Sep 28 '22 08:09

dting


Slightly more concise notation:

sorted_state = sorted(STATE, key=lambda x: x[1])
like image 39
Will Moore Avatar answered Sep 28 '22 08:09

Will Moore