On Dec 19, 8:11 pm, nbv4 <[email protected]> wrote:
> I have a model with a field called date_format. That field is an
> integer value from 1 to 9 corresponding to "D m-j-y", "m-d-Y", etc. I
> have the model defined with a 'choices' set so the database can store
> the number, and a dropdown is created with the choices instead of just
> a textbox field. The problem is that I don't just want "D j-m-Y"
> listed in the dropdown box, I want it to be something more user-
> friendly, like "Day DD/MM/YYYY". Is it possible to change the way
> django makes the dropdown when I invoke {{ form.date_format }} in the
> template, or will I have to just create my own dropdown in the
> template? There are three other dropdown boxes in my project that I
> want to modify in a similar manner as well.

I've done something like this on occasion. I originally mucked about
with setting display values dynamically, but then I realised it's
actually very simple.

Define the standard choices in your model field as always, then when
you come to define the model form just re-declare that field with a
different set of choices - one that has the same database values, but
different display values. For example:

MODEL_CHOICES = (
    (1, 'D j-m-Y'),
    (2, 'm-d-Y'),
)
class MyModel(models.Model):
    date_format = models.IntegerField(choices=MODEL_CHOICES)

DISPLAY_CHOICES = (
    (1, 'Day DD-MM-YYYY'),
    (2, 'MM-DD-YYYY'),
)

class MyModelForm(forms.ModelForm):
    date_format = models.ChoiceField(choices=DISPLAY_CHOICES)
    class Meta:
        model = MyModel

--
DR
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to