I might do it wrong -- How to get and process an object from FormView ?

16 views
Skip to first unread message

Dino

unread,
Dec 11, 2015, 3:31:08 PM12/11/15
to Django users
The form submission and processing as I've done it works, 
but I'm not sure its the right way.

The part I'm concerned about is detailed in these 3 steps:
- I save the form with the data submitted -- form.save()
- then I check request.POST for the name of a (unique) field submitted
- then I query the database for the object with the field specified in request.POST,  to process it further.

Can't I access the object easier ?  ( either before or after saving the form)

class FormViewCustom(FormView):
    form_class = SomeForm
    success_url = reverse_lazy("index")
    template_name = "index.html"

    def form_valid(self, form):
        form.save(commit=True)
        .......................................
        request_field = self.request.POST['fieldx']
        retrieve_model = MyModel.objects.filter(fieldx = request_field).get()

        #and now process the object before returning

Sergiy Khohlov

unread,
Dec 11, 2015, 3:53:54 PM12/11/15
to django-users

Form.save returns object. You don't need tricking with post directly.

11 груд. 2015 22:30 "Dino" <firstl...@googlemail.com> пише:
--
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...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/6913290d-2be5-467e-a307-b03a111c9b2d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages