Django uses curry to create custom form forms

I have a question about using curry function in Django. I successfully used it to create an inlinemodelformset with a custom form, this way:

person_obj, formed = Mother.objects.get_or_create(user=request.user) FormSetClass = inlineformset_factory(Mother, Relation, form=RelationForm, extra=1) FormSetClass.form = staticmethod(curry(RelationForm, person=person_obj)) 

What this does allows me to pass the person to kwargs in the forms init method to filter requests:

 class RelationForm(ModelForm): def __init__(self, *args, **kwargs): person = kwargs.pop('person') qs = Relation.objects.filter(person=person) self.fields['my-field'].queryset = qs 

My question is that I have no idea how this works - I cannot find any documentation on it, and the source code does not actually shed light on it for me. Can someone explain how this works and give some good options for using curry besides this?

+4
source share
1 answer

Pro Django has a good example of how curry works.

It just allows you to execute a function with predefined arguments and save it as a new function for later use only with the necessary arguments. All this without the function working.

In the above example, curry is a huge help since we do not need to instantiate RelationForm to pass the person argument.

+1
source

Source: https://habr.com/ru/post/1495364/


All Articles