Django parameter from views to forms - django

I want to send a parameter from the view to the form with this code.
In the view I call the constructor:
from = FormSet(request.POST or None, prefix='employee', id=id)
The id was given over the url. In the form I define the constructor like this:
class FormSet(SearchForm):
def __init__(self, *args, **kwargs):
try:
id = kwargs.pop('id')
except KeyError:
raise Http404
super(FormSet, self).__init__(*args, **kwargs)
self.fields['employee'] = ModelChoiceField(queryset=Employee.objects.all().filter(id=id))
And I got this error:
__init__() got an unexpected keyword argument 'id'
Do someone know the problem?

I had it once too, i solved it by using:
def __init__(self, id=None, *args, **kwargs):

Related

How to load a form with options from a queryset in Django

I am trying to load a form with user payment options, so this is needing a query set from the users profile.
I have tried initializing the form (below code) with user being required. The issue is if I make self.options when I am initializing. I have also tried creating the choice_field
class ListPaymentOptionsForm(forms.Form):
choice_field = forms.ChoiceField(widget=forms.RadioSelect, choices=options)
def __init__(self, user, *args, **kwargs):
self.options = list(UserPaymentOption.objects
.values_list('last_four', 'last_four')
.filter(user=user, active=True))
super(ListPaymentOptionsForm, self).__init__(self, *args, **kwargs)
The above code gives this error:
choice_field = forms.ChoiceField(widget=forms.RadioSelect, choices=options)
NameError: name 'options' is not defined
Then I have tried adding the options on the view instead like this
form = ListPaymentOptionsForm(user=request.user)
form.fields['choice_field'].choices = list(UserPaymentOption.objects
.values_list('id', 'last_four')
.filter(user=request.user, active=True))
This causes an error with the form being used on post, it seems like because it is trying to validate the value provided is a choice but in the actual form the choice is not set. The reason I believe this is the problem is this is what the form returns as
form=ListPaymentOptionsForm(request.POST)
print(form)
This returns: Choice field:Select a valid choice. 54 is not one of the available choices.
Any input on this would be very appreciated. Thanks.
Nearly there!
Try doing the fields['choice_field'].choices in the constructor.
class ListPaymentOptionsForm(forms.Form):
def __init__(self, user, *args, **kwargs):
super().__init__(*args, **kwargs) # assuming python 3 constructor
self.options = list(UserPaymentOption.objects.values_list('last_four', 'last_four').filter(user=user, active=True))
self.fields['choice_field'] = forms.ChoiceField(widget=forms.RadioSelect, choices=self.options)
Maybe consider having a look at ModelChoiceField instead however, that way you can specify a queryset instead of having to worry about creating a list:
class ListPaymentOptionsForm(forms.Form):
choice_field = forms.ModelChoiceField(widget=forms.RadioSelect, queryset=UserPaymentOption.objects.none())
def __init__(self, user, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields['choice_field'].queryset = UserPaymentOption.objects.filter(user=user, active=True)
EDIT based on comments we can use the kwargs to pass the user which may be better:
class ListPaymentOptionsForm(forms.Form):
choice_field = forms.ModelChoiceField(widget=forms.RadioSelect, queryset=UserPaymentOption.objects.none())
def __init__(self, *args, **kwargs):
user = kwargs.pop('user') # this must be done before super()
super().__init__(*args, **kwargs)
self.fields['choice_field'].queryset = UserPaymentOption.objects.filter(user=user, active=True)
Then instantiate the form to handle POST data:
form = ListPaymentOptionsForm(request.POST, user=user)

Django class based views access passed arguments

How can I log / print passed values to class based view?
Here is my class
class ProjectCreateView(CreateView):
model = Project
form_class = ProjectForm
I have tried appending the following to the class but I'm not seeing anything printed in the console.
def get(self, request, *args, **kwargs):
logging.info(request['name'])
I can't figure out what I'm doing wrong here
Using self.args and self.kwargs works in any generic class-based-view.
class ProjectCreateView(CreateView):
model = Project
form_class = ProjectForm
def get(self, request, *args, **kwargs):
project_name = self.kwargs.get('project_name', None)
# Do something
return super(ProjectUpdateView, self).get(request, *args, **kwargs)
Looking at Classy Class Based Views, a great site for viewing the methods and attributes standard CBVs, shows us why this is. Take a look at this from the TemplateView source code:
#classonlymethod
def as_view(cls, **initkwargs):
"""
Main entry point for a request-response process.
"""
# sanitize keyword arguments
for key in initkwargs:
if key in cls.http_method_names:
raise TypeError("You tried to pass in the %s method name as a "
"keyword argument to %s(). Don't do that."
% (key, cls.__name__))
if not hasattr(cls, key):
raise TypeError("%s() received an invalid keyword %r. as_view "
"only accepts arguments that are already "
"attributes of the class." % (cls.__name__, key))
def view(request, *args, **kwargs):
self = cls(**initkwargs)
if hasattr(self, 'get') and not hasattr(self, 'head'):
self.head = self.get
self.request = request
self.args = args
self.kwargs = kwargs
return self.dispatch(request, *args, **kwargs)
The unpacked args and kwargs passed to the view method are stored as class attributes and available at any post-initialization methods.

Inline formset factory - pass request to child form

I'm facing a quite challenging taks:
I need an inlineformset_factory connecting my ParentEntity to my foreign key-bound ChildEntities.
My ChildEntity contains a foreign key relation I need to filter per logged-in user - so I need the request in the ChildForm.
What I've tried so far:
I tried to use the form= kwarg but I can't pass an instance - just a class. So no way for me to add the request here.
I tried to use the formset= kwarg but when I try to pass the request=request as a kwarg of the inlineformset_factory I get an error (Unexpected kwarg)
Any idea what I can do?
Sometimes asking a colleague is even faster than StackOverflow :)
Here's my solution:
forms.py
class BaseFormSet(BaseInlineFormSet):
def __init__(self, *args, **kwargs):
self.request = kwargs.pop("request", None)
super(BaseFormSet, self).__init__(*args, **kwargs)
views.py
MyFormSet = inlineformset_factory(ParentEntity, ChildEntity, formset=BaseFormSet, form=ChildForm, extra=2, max_num=max_num, can_delete=False)
...
formset = MyFormSet(request.POST, instance=obj, request=request)
you can pass it this way:
MyFormSet = inlineformset_factory(ParentEntity, ChildEntity, formset=BaseFormSet, form=ChildForm, extra=1)
formset = MyFormSet(form_kwargs={'request': request})
Then on your ChildForm:
def __init__(self, *args, **kwargs):
request = kwargs.pop('request', None)
super(ChildForm, self).__init__(*args, **kwargs)

django object is not iterable with custom instance

I am trying to leave my object itself out of the queryset of possible options. Problem is i get the error: 'Country' object is not iterable
Not sure where i am going wrong.
My view:
def edit_country(request, country_id):
country = get_object_or_404(Country, pk=country_id)
country_form = CountryForm(instance=country)
return render(request, 'create_country.html', {'country_form': country_form})
My form init:
def __init__(self, *args, **kwargs):
super(CountryForm, self).__init__(*args, **kwargs)
if 'instance' in kwargs:
self.fields['likes'].queryset = Country.objects.exclude(kwargs['instance'])
self.fields['hates'].queryset = Country.objects.exclude(kwargs['instance'])
Where do i go wrong?
Change the order of the method, so you pop the kwarg first. You are sending the kwarg to super.
def __init__(self, *args, **kwargs):
instance = kwargs.pop('instance', None)
#all other stuff

Passing an argument to the Django ModelForm clean method

I am trying to pass an argument to the clean method of my ModelForm so that I can perform some extra validation on some data.
In my views.py file, I have:
page_data = page_form.cleaned_data(foo="bar")
In my clean_url method, I have:
def clean_url(self, **kwargs):
url = self.cleaned_data['url']
if kwargs['foo'] == url:
query = FlatPage.objects.filter(url=url)
if query.exists():
raise forms.ValidationError(("This url is already being used by the '%s' page.") % (query[0].title))
return url
I keep getting a KeyError for foo. I'm not sure where I'm making a mistake here, as I've passed kwarg variables before, but never to a clean method.
The key lies in passing the paramaters through the ModelForm's init method:
def __init__(self, *args, **kwargs):
self.url = kwargs.pop('url', None)
super(FlatPageForm, self).__init__(*args, **kwargs)
This variable can then be referenced in the clean method by calling self.url
def clean_url(self):
url = self.cleaned_data['url']
if self.url == url:
#do something
else:
#do something else
When using Class Based Views, you can use get_form_kwargs to pass the variable from the view to your form and then to you clean method:
In your view:
def get_form_kwargs(self):
kwargs = super(MyCreateView, self).get_form_kwargs()
kwargs.update({'url': self.kwargs['url']}) # or wherever the url parameter is coming from
return kwargs
In your form:
def __init__(self, *args, **kwargs):
self.url = kwargs.pop('url', None)
super(FlatPageForm, self).__init__(*args, **kwargs)
And then reference self.url in your clean() method.