sFishman
sFishman

Reputation: 133

Sorting A Drop Down Menu in Django With Hard-Coded Choices

I have a few drop down menus that are hard-coded into my model. For example:

CITIZENSHIP_CHOICES = {
    (u'Canada', u'Canada'),
    (u'USA', u'USA'),
    (u'United Kingdom', u'United Kingdom'),
    (u'None', u"None of the above"), 
}

COUNTRY_CHOICES = {
('USA', 'USA'),('Canada', 'Canada'),('Israel', 'Israel'),('UK', 'UK'),('Afghanistan', 'Afghanistan'),('Albania', 'Albania'),('Algeria', 'Algeria'),('Andorra', 'Andorra'),('Angola', 'Angola'),('Antigua and Barbuda', 'Antigua and Barbuda'),('Argentina', 'Argentina'),('Armenia', 'Armenia'),('Australia', 'Australia'),('Austria', 'Austria'),('Azerbaijan', 'Azerbaijan'),('Bahamas', 'Bahamas'),('Bahrain', 'Bahrain'),('Bangladesh', 'Bangladesh'),('Barbados', 'Barbados'),('Belarus', 'Belarus'),('Belgium', 'Belgium'),('Belize', 'Belize'),('Benin', 'Benin'),('Bermuda', 'Bermuda'),('Bhutan', 'Bhutan'),('Bolivia', 'Bolivia'),...
}

SERVICE_LENGTH_CHOICES = {
    (6, "6"),
    (12, "12"),
    (18, "18"),
    (21, "21"),
    (24, "24"),
    (28, "28"),
    (30, "30"),
    (32, "32"),
    (32, "36"),
    (42, "42")
}

All of my fields are in the same model in models.py. Each one of the drop down fields looks like this:

citizenship = models.CharField(max_length=100, choices=CITIZENSHIP_CHOICES, null=True, blank=True)

For some reason though, in my form, the drop downs are in a strange order. I'd like it either to be in the order I entered it in models.py or to order it alphabetically/numerically.

Service Length drop down menu3Country drop down menu2Citizenship drop down menu1

Everything I've found online discusses foreign keys, but I'm not using any foreign keys. Any help is much appreciated.

Upvotes: 1

Views: 211

Answers (1)

sFishman
sFishman

Reputation: 133

See Daniel Roseman's comment above. I used {} instead of ()!

Upvotes: 1

Related Questions