Last active
March 1, 2021 22:22
-
-
Save andrefsp/5115772 to your computer and use it in GitHub Desktop.
Simple way to inject custom parameters on a Django formset without using django.utils.functional.curry()
This file contains 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
""" | |
Simple example on how to inject a custom user parameters onto a formset | |
and its forms. | |
curry(): https://github.com/django/django/blob/1.4.3/django/utils/functional.py#L9 | |
Another answer on: | |
http://stackoverflow.com/questions/622982/django-passing-custom-form-parameters-to-formset | |
""" | |
class BaseAddToBasketForm(forms.Form): | |
product = forms.ModelChoiceField(queryset=Product.objects.none()) | |
quantity = forms.IntegerField(initial=0) | |
def __init__(self, *args, **kwargs): | |
self.user = kwargs.pop('user') # user was passed to a single form | |
super(BaseAddToBasketForm, self).__init__(*args, **kwargs) | |
# create a base Formset class | |
BaseAddToBasketFormSet = formset_factory(BaseAddToBasketForm) | |
class AddToBasketFormSet(BaseAddToBasketFormSet): # sub class it | |
def __init__(self, *args, **kwargs): | |
# create a user attribute and take it out from kwargs | |
# so it doesn't messes up with the other formset kwargs | |
self.user = kwargs.pop('user') | |
super(AddToBasketFormSet, self).__init__(*args, **kwargs) | |
def _construct_form(self, *args, **kwargs): | |
# inject user in each form on the formset | |
kwargs['user'] = self.user | |
return super(AddToBasketFormSet, self)._construct_form(*args, **kwargs) | |
# Instantiate a form for user 'blabla': | |
AddToBasketFormSet(user="blabla") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
One issue that I encountered with this approach is that it doesn't work for the empty_form