On Dec 19, 3:34 pm, Daniel Roseman <[email protected]>
wrote:
> 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
Thats a really elegant solution that I never thought of. Thanks
But what about more complex custom widgets? For instance I have one
field in my Flight model called "plane" that is a OneToOne field
connected to a model called Plane. Plane also has a OneToOne field
that is connected to the User model. When I make a FlightForm, I want
the plane field widget to be a dropdown that contains all the planes
that are connected to the user, and in addition, I want some planes to
have a css class connected to the <option> tag depending on certain
conditions.
The way I do it now, I just create a "Plane.objects.filter
(user=request.user)" object to the template, then construct the widget
in a template, but I think it would be better if I could define it as
a custom widget and just call it with {{ form.plane }}
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---