Reputation: 123
I have a model and a form created with ModelForm. I am using custom validation and I need access to the id of the record I am currently editing. Is there a way to transfer a variable to the form by inserting a custom hidden field somehow or any other way?
Upvotes: 2
Views: 99
Reputation: 31950
You don't need a hidden field. You can transfer variable from view to form:
View:
def some_view(request):
if request.method == 'POST':
some_id = 1
form = SomeForm(data=request.POST, some_id=some_id)
Form:
class SomeForm(ModelForm):
def __init__(self, *args, **kwargs):
if 'some_id' in kwargs:
self.some_id = kwargs.pop('some_id')
super(SomeForm, self).__init__(*args, **kwargs)
Upvotes: 3