In the FormsClass of forms.py, using the currently logged in user information I want to do something.
forms.py
class Form(forms.ModelForm):
def method(self):
model = Model
model.objects.filter(user=[Logged in user]) #I want to use a logged-in user
When creating a Form instance with views.py, pass the logged-in user information as an argument.
views.py
class View(View):
...
form = Form(user=request.user, data=request.POST)
...
forms.py
...
class Form(forms.ModelForm):
def __init__(self, user, *args, **kwargs):
self.current_user = user
super().__init__(*args, **kwargs)
def method(self):
model = Model
model.objects.filter(user=self.current_user)
...
Used when validating with form.
https://stackoverflow.com/questions/5119994/get-current-user-in-django-form
Recommended Posts