Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.
  • Root
  • » Django
  • » How to save data in the model in the second step of a 2-step form? [RSS Feed]

#1 Dec. 6, 2010 12:19:11

Salvatore I.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

How to save data in the model in the second step of a 2-step form?


Hi,
I'm doing a 2-step form, so that in the first step, the user will
upload an image, and in the second step, she will fill in some details
about it.

My problem is that after the second step, the data is not saved in the
model.

Here's some relevant code.

The model:

class Image(models.Model):
uploaded = models.DateTimeField(editable=False)
description = models.TextField()

def save(self):
self.uploaded = datetime.now()
models.Model.save(self)

Two forms, one with an ImageField, and one connected to the Image
model:

class ImageUploadForm(forms.Form):
file = forms.ImageField()

class ImageUploadDetailsForm(forms.ModelForm):
class Meta:
model = Image


The relevant views:

def image_upload_process(request):
"""Process the form"""

form = ImageUploadForm(request.POST, request.FILES)
if not form.is_valid():
return render_to_response("image_upload.html", {"form":form})

file = request.FILES
s3_filename = str(uuid4()) + os.path.splitext(file.name)
store_image_in_s3(file, s3_filename)

image = Image(filename = s3_filename)
image.save()

return render_to_response("image_upload_phase_2.html",
{"image":image,
"s3_images_bucket":settings.S3_IMAGES_BUCKET,
"s3_url":settings.S3_URL,
"form":ImageUploadDetailsForm(),
},
context_instance=RequestContext(request))

def image_upload_process_details(request):
"""Process the second part of the form"""

form = ImageUploadDetailsForm(request.POST)
image_id = request.POST.get('image_id')
image = Image.objects.get(pk=image_id)

if not form.is_valid():
return render_to_response("image_upload_phase_2.html",
{"image":image,
"s3_images_bucket":settings.S3_IMAGES_BUCKET,
"s3_url":settings.S3_URL,
"form":form,
},
context_instance=RequestContext(request))

return HttpResponseRedirect("/show/" + image_id)


As you can see, in the first of the two views, I take the uploaded
image from the request and save it to Amazon's S3. Then redirect to a
template that shows the second form (ImageUploadDetailsForm), plus an
hidden field with the Image's id.

After the second form is done and valid, I would like to save the
information that comes to it to Image, in the same row that has the id
I was passing.

How can I do this?

Thanks in advance!

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

Offline

#2 Dec. 6, 2010 16:10:28

Wayne S.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

How to save data in the model in the second step of a 2-step form?


> After the second form is done and valid, I would like to save the
> information that comes to it to Image, in the same row that has the id
> I was passing.
>
> How can I do this?
>
>
>
image.description = information user provided
image.save()

By the way, your overridden save() method looks a bit odd. It needs to call
save() on its superclass as well.


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

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

Offline

#3 Dec. 6, 2010 17:05:00

Salvatore I.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

How to save data in the model in the second step of a 2-step form?


On Dec 6, 6:09 pm, Wayne Smith <wayne.tuxro...@gmail.com> wrote:
> > After the second form is done and valid, I would like to save the
> > information that comes to it to Image, in the same row that has the id
> > I was passing.
>
> > How can I do this?
>
> image.description = information user provided
> image.save()

Thanks, that's fine, but I was wondering if there was a more scalable
way. I have more fields than just "description", so it would be nice
if they could be all saved with one method call, so I don't have to
maintain this code in case I change the model.

> By the way, your overridden save() method looks a bit odd.  It needs to call
> save() on its superclass as well.

But I do:
models.Model.save(self)

Isn't that what you meant?

And how do I get that information provided by the user?
Something like:
Image i = ImageUploadDetailsForm.save(commit=False)
?

Thanks.

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

Offline

#4 Dec. 7, 2010 03:19:08

Wayne S.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

How to save data in the model in the second step of a 2-step form?


>
> Thanks, that's fine, but I was wondering if there was a more scalable
> way. I have more fields than just "description", so it would be nice
> if they could be all saved with one method call, so I don't have to
> maintain this code in case I change the model.
>
>
If you have multiple fields describing your image, and you are concerned
that more fields will be added/removed at a later time and don't want to
fiddle with your Image model fields, then it would probably be prudent to
factor out these fields to their own model, and hold a relationship (1-to-1,
probably) from the image model to the description model.


> > By the way, your overridden save() method looks a bit odd. It needs to
> call
> > save() on its superclass as well.
>
> But I do:
> models.Model.save(self)
>
> Isn't that what you meant?
>

You're correct in that you call the superclass, I looked over it too
quickly. However, the way you are doing it won't pass any of the args or
kwargs up to the superclass, and will probably give you problems. Look herehttp://docs.djangoproject.com/en/dev/topics/db/models/#overriding-model-methodstoget an example.


> And how do I get that information provided by the user?
> Something like:
> Image i = ImageUploadDetailsForm.save(commit=False)
> ?
>

You get the info from the user by accessing the values in request.POST,
which is a dictionary-like object with all the POST parameters. If you are
using a ModelForm, you might be able to get some things "automagically", but
you can always access them in request.POST no matter how the form is set
up.

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

Offline

  • Root
  • » Django
  • » How to save data in the model in the second step of a 2-step form? [RSS Feed]

Board footer

Moderator control

Enjoy the 24th of October
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support