R
R
Rrooom2014-08-22 14:30:09
Django
Rrooom, 2014-08-22 14:30:09

What is the best way to add data to a Django form?

There is a model:

class A(Model):
    name=CharField()
    user = ForeignKey(User)

The form:
Class FA(ModelForm):
    model=A
   fields = ['name']

What is the best way to append to the current user's form data without completely overriding form save with cbv? Those. before calling form_valid, you need to add information about the current user to the form. How and where?
class AUpdateView(LoginRequiredUpdateView):
    model = A
    form = FA
    success_url = '/'

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey K, 2014-08-22
@mututunus

class AUpdateView(LoginRequiredUpdateView):
    def get_form_kwargs(self):
        kwargs = super(AUpdateView, self).get_form_kwargs()
        kwargs['data'].update({'user': self.request.user})
        return kwargs

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question