2

There are several questions on stackoverflow related to this topic but none of them explains whats happening neither provides working solution.

I need to pass user's first name as an argument to Django ModelForm when rendering template with this form.

I have some basic form:

class MyForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        self.first_name = ???
        super(MyForm, self).__init__(*args, **kwargs)
    class Meta:
        model = MyModel
        fields = [***other fields***, 'first_name']

Here's my sample class-based view:

class SomeClassBasedView(View):
def get(self, request):
    user_first_name = request.user.first_name
    form = MyForm(user_first_name)
    return render(request, 'my_template.html', {'form': form})

What do I pass to MyForm when initialising it and how do I access this value inside the __init__ method?

I want to use 'first_name' value as a value for one of the fields in template by updating self.fields[***].widget.attrs.update({'value': self.first_name}).

6
  • It's impossible to answer this question without knowing what you want to do with that value. Why do you want to pass it in the first place? Are you looking for initial data? Commented Jan 11, 2018 at 14:49
  • Do you really want to include first_name in fields? If you just want to use the value of request.user.first_name, then the common approach is to exclude it from the form, and set the value in the view. Commented Jan 11, 2018 at 14:50
  • @DanielRoseman Thanks for your note, I've added the explanation why do I need this field. Commented Jan 11, 2018 at 14:53
  • @Alasdair I'm trying to avoid this solution because I'm editing attributes of fields inside the Form so I render it all at once in the template. But if I pass it as argument to template I'd have to render the entire form manually. Commented Jan 11, 2018 at 14:55
  • 2
    That's not what placeholders are for. A placeholder will vanish as soon as the user starts typing into the field (like the Search box at the top of this page), and won't be submitted if they don't replace it with real text. I think you want that field to be prepopulated with the firstname instead; as I said above, that's what initial data is for. Commented Jan 11, 2018 at 14:55

2 Answers 2

2

The solution is as simple as passing initial data dictionary to MyForm for fields you need to set initial value.

This parameter, if given, should be a dictionary mapping field names to initial values. So if MyModel class has a field my_field and you want to set its initial value to foo_bar then you should create MyForm object with initial parameters as follows:

form = MyForm(initial={'my_field': 'foo_bar'})

A link to Django documentation on this topic.

Sign up to request clarification or add additional context in comments.

Comments

1

You can pass the parameter instance to your form like this:

obj = MyModel(...)   
form = MyForm(instance=obj)

If obj has a user first name it will be attached to your form.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.