On Monday, April 8, 2013 12:22:34 PM UTC-4, Nora Olsen wrote:
>
> Hi,
>
> I'm a new user to Django. 
>
> I'm trying to understand how does an UpdateView works when during a POST. 
> Is there some state carried between the GET and POST, because I don't see 
> the pk in the form. How does the form save() knows that it should be 
> updating instead of inserting?
>
> The reason I'm asking is because I am having trouble trying to save a 
> formset that is used in a view together with the main form. I posted the 
> question on stackoverflow: 
> http://stackoverflow.com/questions/15877199/using-formset-with-contenttype
>
> I am trying to save Item with multiple Pictures in a single form.
>
> I can easily save the item form in the Post method of the view but I can't 
> save the formsets without the pk. In my template, I have the following:
>
> <form action="" method="post" enctype="multipart/form-data">
> {% for field in form %}
>     # do something
> {% endfor %}
>
> {{ picture_formset.management_form }}
> {% for form in picture_formset.forms %}
>     # do something
> {% endfor %}
> <input  name="commit" type="submit" value="Submit" />
> </form>
>
>
> And my view:
>
>
>     class ItemUpdateView(UpdateView):
>
>     form_class = ItemCreateForm
>     template_name = 'item/new.html'
>     model = Item
>     success_url = '/items/'
>     def get_context_data(self, **kwargs):
>         context = super(ItemUpdateView, self).get_context_data(**kwargs)
>         item = context['object']
>         # Dont' create any extra forms when showing an update view
>         PictureFormSet = formset_factory(PictureForm, extra=0)
>         return {'form': kwargs['form'],
>                 'picture_formset': UploadFormSet(initial = [ 
> model_to_dict(a) for pic in item.pictures.all()])}
>     def post(self, request, *args, **kwargs):
>        self.object = self.get_object()
>        item_form = ItemCreateForm(request.POST, instance=self.object)
>        if item_form.is_valid():
>            item = item_form.save(commit=False)
>            item.save()
>            # How do I update the pictures? 
>
>
> I did manage to solve this by adding the pk as a HiddenWidget and 
> obtaining the Picture model before updating the keys/values and saving it.
>
> But I had to create 2 type of forms: with and without the pk for the 
> CreateView and UpdateView.
>
> Is this the correct approach? 
>


Hi Nora, the way it works is that it uses get_object() method to load the 
object; at the top of post() method,
it has the line: self.object = self.get_object().

If you need to use modelformset together with a form or modelform in one 
view, you might
want to look at my mcbv library; it has both a modelformset CBV and also 
allows to easily
combine different types of views together.

The portfolio tutorial has a good examples of doing just that: 
http://lightbird.net/dbe2/portfolio.html

mcbv library itself currently lives here: https://github.com/akulakov/django

 -ak

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


Reply via email to