Hello,
I have some troubles with inlineformset while I want to specify the queryset to be used.
I have the following function:
def get_business_queryset_and_formset(request, view_as_customer, company=None, framework_agreement=None, data=None):
# Détermine le champ de filtrage et la valeur de filtrage en fonction des arguments fournis
if view_as_customer == 'checked':
filter_field = 'customer'
else:
filter_field ='beneficiary'
if company:
view_as_customer = 'checked'
business_queryset = Business.objects.filter(customer=company)
BusinessFormSet = inlineformset_factory(
Company,
Business,
fk_name=filter_field,
extra=0,
can_delete=False,
form=BusinessForm
)
elif framework_agreement:
view_as_customer = 'checked'
business_queryset = Business.objects.filter(framework_agreement=framework_agreement)
BusinessFormSet = inlineformset_factory(
Company,
Business,
fk_name=filter_field,
extra=0,
can_delete=False,
form=BusinessForm
)
business_queryset = business_queryset.order_by(
Case(
When(ponderation__label="Terminée", then=Value(0)),
When(ponderation=0, then=Value(1)),
default='ponderation__weight',
output_field=IntegerField()
).desc(),
'-ponderation__weight'
)
business_formset = BusinessFormSet(instance=company, queryset=business_queryset, data=data)
return business_formset
It works fine with a company. I get all the businesses related to this company.
Unfortunately, it does not work with a framework_agreement. I want to restrict my view to the businesses of a given company which are related to the framework.
I appears that the queryset is ok, but the business_formset is empty.
Any help is welcome