Django pass User instance to Forms when form is created - python

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)
ref = forms.ModelChoiceField(queryset=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

If you do this code in field declaration section like
class QuestionForm(forms.Form):
ref = forms.ModelChoiceField(queryset=Study.objects.filter(owner=...)
then it will not work because it still doesn't have self variable.
You can do this in init method like this
class QuestionForm(forms.Form):
ref = forms.ModelChoiceField()
def __init__(self, *args, **kwargs):
super(MyForm, self).__init__(*args, **kwargs)
self.fields['ref'].queryset = Study.objects.filter(owner=request.user)

Related

Django 'TestForm' object has no attribute 'fields'

I'm using django:
I'm trying to pass a list of tuples from views.py to a dropdown box form but I get this attribute error
forms.py
import logging
from django import forms
log = logging.getLogger(__name__)
class TestForm(forms.Form):
def __init__(self, *args, **kwargs):
testlist = kwargs.pop('testlist',None)
log.info(regionlist)
self.fields['testlist'] = forms.ChoiceField(choices=testlist)
super(TestForm, self).__init__(*args, **kwargs)
views.py
form = forms.RegionForm(regionlist=data)
Am I using the right method to pass variables between views.py and forms.py?
You need to call super first, so that the superclass sets up the fields attribute.
def __init__(self, *args, **kwargs):
testlist = kwargs.pop('testlist', None)
log.info(regionlist)
super(TestForm, self).__init__(*args, **kwargs)
self.fields['testlist'] = forms.ChoiceField(choices=testlist)

Python - Django - Pass Argument To Form Clean Method

I am trying to pass a variable to a ModelForm clean method using __init__ arguments but have had no success so far - I looked at various posts on StackOverflow but none seemed to help.
My code is the following:
forms.py
class property_booking_form(forms.ModelForm):
check_in_date = forms.DateField(widget=SelectDateWidget)
check_out_date = forms.DateField(widget=SelectDateWidget)
class Meta:
model = Properties_bookings
fields = ['check_in_date', 'check_out_date']
def __init__(self, property_id):
self.property_id = property_id
super(property_booking_form, self).__init__(self, property_id)
def clean(self):
check_in_date = self.cleaned_data.get('check_in_date')
check_out_date = self.cleaned_data.get('check_out_date')
property_min_nights = Properties.objects.get(id=self.property_id).property_minimum_nights
...
views.py
def view(request):
...
if request.method == 'POST':
booking_form = property_booking_form(request.POST, property_id=property_id)
if booking_form.is_valid():
...
else:
booking_form = property_booking_form(property_id=property_id)
return render(...)
This raises the following error:
'property_booking_form' object has no attribute 'get'
Which seems to be related to the widget as per the error description:
Exception Location:
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/forms/widgets.py in value_from_datadict, line 1058
The form works fine without the overriding __init__.
Does anyone know what would be the underlying cause of this issue?
Thanks.
Your __init__ method should accept *args and **kwargs, you should pass these when you call the superclass' __init__ method, rather than self and property_id.
def __init__(self, property_id, *args, **kwargs):
self.property_id = property_id
super(property_booking_form, self).__init__(*args, **kwargs)
You also need to change the way you instantiate the form in the view, since property_id is the first argument. For example:
if request.method == 'POST':
booking_form = property_booking_form(property_id=property_id, data=request.POST)
Alternatively, you can remove property_id from the signature, and pop it from kwargs. In this case, no changes to the views are required.
def __init__(self, *args, **kwargs):
self.property_id = kwargs.pop('property_id')
super(property_booking_form, self).__init__(*args, **kwargs)
This has been solved by amending __init__ as follows:
def __init__(self, *args, **kwargs):
self.property_id = kwargs.pop('property_id', None)
super(property_booking_form, self).__init__(*args, **kwargs)

Stuck in passing and catching kwargs

Python 3.5.2
Could you help me understand how parameters are transmitted to the method.
Just in case: this is Django but the question seems to be just about Python.
As far as I can catch from the Django's documentation: if I pass form_kwargs to a formset like this, several forms will be created. And in the form I expect to catch the parameter in **kwargs.
class MyArticleForm(ArticleForm):
def __init__(self, *args, **kwargs):
self.user = kwargs.pop('user')
super(MyArticleForm, self).__init__(*args, **kwargs)
formset = ArticleFormSet(form_kwargs={'user': user})
Well, this is not working:
File "/home/michael/workspace/formsets/general/forms.py", line 15, in __init__
self.user = kwargs.pop('user')
KeyError: 'user'
If I step in the debugger: kwargs definitely doesn't contain any 'user'. And args is an empty tuple.
But this works:
class MyArticleForm(ArticleForm):
def __init__(self, user=None, *args, **kwargs):
pass
And this works:
class MyArticleForm(ArticleForm):
def __init__(self, user, *args, **kwargs):
pass
Well, I can't understand how can I catch this parameter in using kwargs?
///////////
ADDED LATER
ArticleFormSet = formset_factory(MyArticleForm)
def formset_factory(form, formset=BaseFormSet, extra=1, can_order=False,
can_delete=False, max_num=None, validate_max=False,
min_num=None, validate_min=False):
pass
class BaseFormSet(object):
def __init__(self, data=None, files=None, auto_id='id_%s', prefix=None,
initial=None, error_class=ErrorList, form_kwargs=None):
...
self.form_kwargs = form_kwargs or {}
....

Passing arguments to a form obtained by 'getattr()'

I'm creating a view which loads differents forms according to an argument given using the getattr() function:
form = getattr(forms, service.form)
but in the form I need my username to filter my files, so I have this:
class MyForm(forms.Form):
filename = forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple)
K = forms.CharField(label='K', max_length=1)
fullOut = forms.CharField(label='fullOut', max_length=1)
def __init__(self, *args, **kwargs):
super(MyForm, self).__init__(*args, **kwargs)
self.user = kwargs.pop('user', None)
self.fields['filename'].queryset = userFile.objects.filter(self.user)
The problem is that I don't know how to pass the 'request.user' in my getattr() funtion. I know that if it was static it should be something like:
form = MyForm(user=request.user)
But I have tried somethings like:
form = getattr(forms, service.form, user=request.user)
And it doesn't work.I'm trying this but any idea of how list user files in a form will be welcomed.
Thanks in advance.
This doesn't have anything to do with you using getattr, the problem is in your __init__ method. You need to pop user before calling super().
def __init__(self, *args, **kwargs):
self.user = kwargs.pop('user', None)
super(MyForm, self).__init__(*args, **kwargs)
You should instantiate the form as you usually do:
form = MyForm(user=request.user)
It doesn't matter whether MyForm is declared in the same module:
class MyForm(forms.Form):
my_field = forms.CharField()
form = MyForm(user=request.user)
or if you get the form class dynamically using getattr
MyForm = getattr(forms, service.form)
form = MyForm(user=request.user)

Django: Filter ModelChoiceField by user

I have a model along with a ModelForm based on that model. The ModelForm contains a ModelMultipleChoice field, which I specify in the subclass of my ModelForm:
class TransactionForm(ModelForm):
class Meta:
model = Transaction
def __init__(self, *args, **kwargs):
super(TransactionForm, self).__init__(*args, **kwargs)
self.fields['category'] = forms.ModelChoiceField(queryset=Category.objects.filter(user=user))
As you can see, I need to filter the Category queryset by user. In other words, users should only see their own categories on the drop down. But how can I do this when user, or more specifically, request.user, is not available in a Model instance?
Edit: Adding my subclass of the CBV:
class TransUpdateView(UpdateView):
form_class = TransactionForm
model = Transaction
template_name = 'trans_form.html'
success_url='/view_trans/'
def get_context_data(self, **kwargs):
context = super(TransUpdateView, self).get_context_data(**kwargs)
context['action'] = 'update'
return context
I tried form_class = TransactionForm(user=request.user) and I'm getting a NameError saying that request was not found.
You can pass request.user to form init in view:
def some_view(request):
form = TransactionForm(user=request.user)
and add user parameter to form __init__ method (or pop it from kwargs in form):
class TransactionForm(ModelForm):
class Meta:
model = Transaction
# def __init__(self, *args, **kwargs):
# user = kwargs.pop('user', User.objects.get(pk_of_default_user))
def __init__(self, user, *args, **kwargs):
super(TransactionForm, self).__init__(*args, **kwargs)
self.fields['category'] = forms.ModelChoiceField(
queryset=Category.objects.filter(user=user))
update: in class based views you can add extra parameter to form init in get_form_kwargs:
class TransUpdateView(UpdateView):
#...
def get_form_kwargs(self):
kwargs = super(YourView, self).get_form_kwargs()
kwargs.update({'user': self.request.user})
return kwargs

Categories

Resources