Last active
March 1, 2023 11:28
-
-
Save rg3915/ae0e27086da4bbb06de313b16a92a4b7 to your computer and use it in GitHub Desktop.
Passando User no forms. usuário logado usuario form usuario logado - form kwargs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def __init__(self, *args, **kwargs): | |
self.user = kwargs.pop('user', None) | |
super(MyForm, self).__init__(*args, **kwargs) | |
if user.is_authenticated: | |
my_field = MyModel.objects.filter(user=user) | |
# ... | |
self.fields['bar'].choices = qs_choices |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
form = MyForm(request.POST or None, user=request.user) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://sayari3.com/articles/16-how-to-pass-user-object-to-django-form/