Django: Passing url slugs to formset queryset - django

I am trying to change the queryset of my formset with this 'alternative approach':
Alternatively, you can create a subclass that sets self.queryset in
init:
from django.forms import BaseModelFormSet from myapp.models import Author
class BaseAuthorFormSet(BaseModelFormSet):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.queryset = Author.objects.filter(name__startswith='O')
Now my problem is, that I have to pass variables into this queryset. Therefore I tried to adjust BaseAuthorFormSet in my forms.py like that:
class BaseAssignAttendeeFormSet(BaseModelFormSet):
def __init__(self, order_reference, access_key, *args, **kwargs):
super().__init__(*args, **kwargs)
self.queryset = Attendee.objects.filter(
order__order_reference=order_reference,
order__access_key=access_key,
)
My function-based-view in views.py starts like that: def assign_attendee(request, order_reference, access_key): order_reference and access_key are slugs that come from the url.
Anyone knows the way to pass these two to my queryset?

You can pass arguments during formset initializing in the view:
MyFormSet = modelformset_factory(MyModel, fields=('name', 'title'), formset=BaseAssignAttendeeFormSet)
formset = MyFormSet(order_reference, access_key, #Other orguments here)
In formset you need to call superclass's __init__ method:
class BaseAssignAttendeeFormSet(BaseModelFormSet):
def __init__(self, order_reference, access_key, *args, **kwargs):
super().__init__(*args, **kwargs)
self.queryset = Attendee.objects.filter(
order__order_reference=order_reference,
order__access_key=access_key,
)

Related

Use self in ModelForm fields

I have a Formview and pass the pk to the ModelForm. In the ModelForm i am not able to use self.pk in the queryset i define for the field:
views.py
[...]
def get_form_kwargs(self):
# Pass extra kwargs to DetailProductForm
kwargs = super(DetailProduct, self).get_form_kwargs()
kwargs.update({'pk': self.kwargs['pk']})
return kwargs
forms.py
class DetailProductForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
# get the pk from the FormView at set it as self.pk
self.pk = kwargs.pop('pk', None)
super(DetailProductForm, self).__init__(*args, **kwargs)
# use self.pk in my queryset (there are more fields who use self.pk, this is just one as an example)
field = forms.ModelChoiceField(queryset=Configuration.objects.filter(product__pk=self.pk), widget=forms.RadioSelect)
class Meta:
model = Configuration
fields = ['field']
NameError: name 'self' is not defined
How can i use self.pk for the fields?
Found a solution. I actually do not even need a ModelForm anymore.
class DetailProductForm(forms.Form):
field = forms.ModelChoiceField(queryset=None, widget=forms.RadioSelect)
def __init__(self, *args, **kwargs):
self.pk = kwargs.pop('pk', None)
super().__init__(*args, **kwargs)
self.fields['field'].queryset = Configuration.objects.filter(product__pk=self.pk))

how to access session variable inside form class

Hi i have a session variable city, how to access it inside form class.
Something like this
class LonginForm(forms.Form):
current_city=request.city
A Form has by default no access to the request object, but you can make a constructor that takes it into account, and processes it. For example:
class LonginForm(forms.Form):
def __init__(self, *args, request=None, **kwargs):
super(LonginForm, self).__init__(*args, **kwargs)
self.request = request # perhaps you want to set the request in the Form
if request is not None:
current_city=request.city
In the related views, you then need to pass the request object, like:
def some_view(request):
my_form = LonginForm(request=request)
# ...
# return Http Response
Or in a class-based view:
from django.views.generic.edit import FormView
class LonginView(FormView):
template_name = 'template.html'
form_class = LonginForm
def get_form_kwargs(self, *args, **kwargs):
kwargs = super(LonginView, self).get_form_kwargs(*args, **kwargs)
kwargs['request'] = self.request
return kwargs

django SetPasswordForm and AdminPasswordChangeForm

hi im trying to use the above forms - but i get
__init__() takes at least 2 arguments (1 given)
i get to the form that it should show but it never save me the new password
i also needed to change the:
def __init__(self, user, *args, **kwargs):
self.user = user
super(AdminPasswordChangeForm, self).__init__(*args, **kwargs)
to:
def __init__(self, *args, **kwargs):
super(AdminPasswordChangeForm, self).__init__(*args, **kwargs)
since it doesnt get a user arg.
any ideas why?
thx
============================ edit =============================================
class set(FormView):
model = User
form_class = AdminPasswordChangeForm
template_name = 'set.html'
def dispatch(self, request, *args, **kwargs):
return super(set, self).dispatch(request, *args, **kwargs)
def get_form_kwargs(self):
kwargs = super(set, self).get_form_kwargs()
kwargs['user_to_update'] = the user
return kwargs
the init:
def __init__(self, *args, **kwargs):
self.user = kwargs['user_to_update']
kwargs.pop('user_to_update')
super(AdminPasswordChangeForm, self).__init__(*args, **kwargs)
Use the existing form. Overide the view's get_form_kwargs method to pass the expected arguments to the form, instead of changing the __init__ method, which will break other things.
In order to save the password, you need to override the form_valid method and call form.save().
For create and update views, you don't always need to override form_valid, because the default behaviour is to save the form and redirect. For FormView, the default behaviour is simply to redirect, so you do have to override it to get it to do anything useful.
class SetPasswordView(FormView):
form_class = AdminPasswordChangeForm
template_name = 'set.html'
success_url = '/thanks/'
def get_form_kwargs(self):
kwargs = super(set, self).get_form_kwargs()
kwargs['user_to_update'] = the user
return kwargs
def form_valid(self, form):
form.save()
return super(SetPasswordView, self).form_valid(form)

Passing variables when instantiating a form class using FormWizard

I have a simple form which uses a queryset that is set dynamically:
class FooForm(forms.Form):
bar = forms.ModelChoiceField(queryset = Bar.objects.none())
def __init__(self, queryset=None, *args, **kwargs):
super(FooForm, self).__init__(*args, **kwargs)
self.fields['bar'].queryset = queryset
I'd like to use this as one of my forms in a FormWizard, but I can't figure out how I can get FormWizard to pass on the queryset. Is this possible at all?
I think you could override the "get_form" method for that particular wizard
class MyWizard(FormWizard):
def __init__(self, *args, **kwargs):
self.querysets = kwargs.pop('querysets', None)
super(self, MyWizard).__init__(*args, **kwargs)
def get_form(self, step, data=None, *args, **kwargs):
return self.form_list[step](data, prefix=self.prefix_for_step(step), initial=self.initial.get(step, None), queryset=self.querysets.get(step, None))
def done(self, *args, **kwargs): pass

django: how to access current request user in ModelForm?

In my implementation of ModelForm, I would like to perform different types of validation checks based on whether current user is superuser. How can I access the current request user?
If you're using Class Based Views (CBVs) then passing an extra argument in the form constructor (e.g. in get_forms_class) or in form_class will not work, as <form> object is not callable will be shown.
The solution for CBVs is to use get_form_kwargs(), e.g.:
views.py:
class MyUpdateView(UpdateView):
model = MyModel
form_class = MyForm
# Sending user object to the form, to verify which fields to display/remove (depending on group)
def get_form_kwargs(self):
kwargs = super(MyUpdateView, self).get_form_kwargs()
kwargs.update({'user': self.request.user})
return kwargs
forms.py:
class MyForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
self.user = kwargs.pop('user') # To get request.user. Do not use kwargs.pop('user', None) due to potential security hole
super(MyForm, self).__init__(*args, **kwargs)
# If the user does not belong to a certain group, remove the field
if not self.user.groups.filter(name__iexact='mygroup').exists():
del self.fields['confidential']
you can pass the user object as an extra argument in the form constructor.
e.g.
f = MyForm(user=request.user)
and the constructor will look like:
class MyForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
self.user = kwargs.pop('user',None)
super(MyForm, self).__init__(*args, **kwargs)
and then use user in the clean_XX forms as you wish
My small addition,
I had a requirement where one of the model choice fields of the form is dependent on the request.user, and it took a while to take my head around.
The idea is that
you need to have a __init__ method in the model form class,
and you access the request or other parameters from the arguments of the __init__ method,
then you need to call the super constructor to new up the form class
and then you set the queryset of the required field
code sample
class CsvUploadForm(forms.Form):
def __init__(self, *args, **kwargs):
user = kwargs.pop('user')
super(CsvUploadForm, self).__init__(*args, **kwargs)
self.fields['lists'].queryset = List.objects.filter(user=user)
lists = forms.ModelChoiceField(queryset=None, widget=forms.Select, required=True)
as you can see, the lists variable is dependent on the current user, which is available via request object, so we set the queryset of the field as null, and its assigned dynamically from the constructor later.
Take a look into the order of the statements in the above code
you can pass the user variable like this from the view file
form = CsvUploadForm(user=request.user)
or with other POST, FILE data like below
form = CsvUploadForm(request.POST, request.FILES, user=request.user)
You may reference the user object using the instance attribute within the instance it self.
Ex; self.instance.user
class StatusForm(ModelForm):
# def __init__(self, *args, **kwargs):
# self.user = kwargs.pop('user', None)
# super(StatusForm, self).__init__(*args, **kwargs)
class Meta:
model = Status
fields = [
'user',
'content',
'image'
]
def clean_content(self):
content = self.cleaned_data.get("content", None)
if len(content) > 240:
raise ValidationError(f"Hey {self.instance.user.username}, the content is too long")
return content
This worked for me, when I am not sending form in context explicitly in get_context_data:
views.py
class MyView(FormView):
model = MyModel
form_class = MyForm
def get_form_kwargs(self):
kwargs = super(MyView, self).get_form_kwargs()
kwargs.update({'user': self.request.user})
return kwargs
form.py
class MyForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
self.user = kwargs.pop('user')
super(MyForm, self).__init__(*args, **kwargs)
if not self.user.groups.filter(name__iexact='t1_group').exists():
del self.fields['test_obj']
When sending form explicitly in get_context_data we can use and this is forms.Form :
views.py
class MyView(FormView):
model = MyModel
form_class = MyForm
def get_context_data(self, **kwargs):
context = super(MyView, self).get_context_data(**kwargs)
context['form'] = self.form_class(self.request.user)
return context
forms.py
class MyForm(forms.Form):
def __init__(self, user,*args, **kwargs):
super(MyForm, self).__init__(*args, **kwargs)
if not user.groups.filter(name__iexact='t1_group').exists():
del self.fields['test_obj']