Gladys -

Thanks you so much! Originally I was passing the object but I was
leaving off the instance=
That didn't work out so well.
Jim

On Apr 10, 2:20 pm, gladys <gladysbi...@gmail.com> wrote:
> Hello,
>
> TestForm is a ModelForm, therefore you initialize it by passing an
> object instance.
> obj = Test.objects.get(id=id)
> form = TestForm(instance=obj)
>
> Here is a sample view with proper usage of modelforms:
>
> def formrender(request, id=None):
>     form = TestForm() # TestForm is a ModelForm
>     test = None
>     if id:
>         test = Test.objects.get(id=id) # note- should try-except here
>         form = TestForm(instance=test)
>     if request.method == 'POST':
>         form = TestForm(request.POST, instance=test)
>         form.save()
>     return render_to_response('temp2.html',{'form':form},
>         context_instance=RequestContext(request))
>
> Hope it helps.
>
> --
> Gladyshttp://bixly.com
>
> On Apr 11, 12:34 am, jim_rain <j...@rainville.net> wrote:
>
>
>
> > Hi -
>
> > I'm using Django 1.3 and I have a model defined with a DateTimeField
> > and a corresponding form that uses a SpliteDateTimeField to format the
> > date and time the way I want it to look. The definition looks like
> > this:
>
> > # ----------- Code snippet _____________________________#
> > class Test(models.Model):
> >     name            = models.CharField(max_length=200)
> >     description     = models.CharField(max_length=1024, blank=True)
> >     start_time      = models.DateTimeField('start date/time')
>
> > class TestForm(ModelForm):
> >     start_time = SplitDateTimeField(input_time_formats=['%I:%M %p',
> > '%H:%M:%S', '%H:%M'],
>
> > widget=SplitDateTimeWidget(date_format='%m/%d/%Y', time_format='%I:%M
> > %p'))
> >     class Meta:
> >         model = Test
> >         widgets = { 'description': Textarea(attrs={'cols': 40, 'rows':
> > 5}), }
>
> > # ----------- End snippet _____________________________#
>
> > This works perfectly if I'm adding new instances of Test, the date and
> > time format the way I want it and they go into the database correctly.
> > The problem I'm having is that I would like to have an edit page to
> > edit the item in the data base. So in my view I have this:
>
> > # ----------- Code snippet _____________________________#
> > def test_edit(request, test_id):
> >     if request.method == 'POST': # If the form has been submitted...
> >         # Edited out for brevity.
> >     else:
> >         test = Test.objects.get(id=test_id)
> >         test_dict = model_to_dict(test)
>
> > logging.info('Test_dict-------------------------------------------')
> >         logging.info(test_dict)
> >         form = TestForm(test_dict) # An unbound form
>
> >     return render_to_response('tester/edit_test.html', {'form':
> > form,},
>
> > context_instance=RequestContext(request))
> > # ----------- End snippet _____________________________#
>
> > When I do this I can look in the log and the start_time is a date time
> > tuple as it should be. But in my form the fields for date and time are
> > not populated with the values that were in the database and that
> > showed up in the log.
> > Another curious thing is that when I create a new TestForm object and
> > give it an initial value like this:
> >        form = TestForm(initial={'start_time':
> > datetime.datetime.now(), })
> > It works just fine - the date and time are populated in the form with
> > the current date and time.
>
> > What am I doing wrong here? I have a feeling I have to do something
> > with over riding the clean method of the form but I'm now sure how.
>
> > Thanks for any help.
> > Jim

-- 
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