Django pass User instance to Forms when form is created

106 views
Skip to first unread message

Ernest Appiah

unread,
Oct 14, 2015, 5:50:56 AM10/14/15
to Django developers (Contributions to Django itself)



I have a Django form and l would like to pass a user instance when the form is created

First Approach

This is where l create the form and pass the instance of the user:

form = QuestionForm(request.user, request.POST)

And inside the QuestionForm

def __init__(self, user, *args, **kwargs):
    super(QuestionForm, self).__init__(*args, **kwargs)
    self.data = user
    log.info(self)

Study.objects.filter(owner = self.data.id))

Second Approach This is where l create the form and pass the request:

form = QuestionForm ( ..., request=request)

And inside the QuestionForm

def __init__(self, *args, **kwargs):
    self.request = kwargs.pop("request")
    super(MyForm, self).__init__(*args, **kwargs)

Study.objects.filter(owner = self.request.user.id))

Now l am getting an error that self is not define and as such l cannot get the user id to query the Study class

Any help would be much appreciated

Tim Graham

unread,
Oct 14, 2015, 7:57:50 AM10/14/15
to Django developers (Contributions to Django itself)
Please write to django-users, this mailing list is for the development of Django itself.
Reply all
Reply to author
Forward
0 new messages