On Thu, 2009-07-16 at 07:37 -0700, clea wrote:
> Hello-
> So I have the following issue and have not been able to find a
> solution to it anywhere! I have the following ModelForm object:
> 
> class OptionForm(ModelForm):
>       class Meta:
>               model = Option
> 
> based on the following Option object:
> 
> class Option(models.Model):
>       stem = models.ForeignKey(Stem, null=True, blank=True)
>       label = models.CharField(max_length=500)
>       value = models.CharField(max_length=255)
> 
> 
> Now, in views.py I return the following when I request the form:
> option_db = Options.object.get(id=1)
> option = OptionForm(instance=option_db)
> return render_to_response('create_edit_option.html',
> {'option':option})
> 
> Everything works great, as expected, on post all data is bound
> correctly and when form is initially displayed all the information
> from the database initially part of the OptionForm object is displayed
> correctly.  My problem is that there are many instances where, within
> the template, I want to access the inital value in "label", for
> example.  If I do {{ option.label }} I get the string "<input
> type="text"..." etc, as documented, BUT say the label for option with
> id=1 is "Option One!", then {{ option.label }} prints <input
> type="text" name="whatever" value="OptionOne!"/>, but I just want the
> string "Option One!".  Same thing for a select box.  Is this
> possible??
> 
> Thanks!
> Clea
> 

It is hard to do it for a form, because the initial data isn't coupled
tightly to the form fields. This is a smart design, it makes lots of
things much easier to do, but its tricky when you want to actually get
the value of a field. 

In this particular case, you could look at {{ form.instance.label }},
but this could return the wrong thing - if the form was not submitted
successfully, then this would return the starting value of the label,
rather than the potentially different bound value, which you could find
in {{ form.data.label }}

I guess a pattern for this could be:

{% if form.data %}
  {{ form.data.label }}
{% else %}
  {% form.instance.label %}
{% endif %}

but it isn't that pretty - any improvements?

Cheers

Tom



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

Reply via email to