# inlineformset with a queryset

**URL:** <https://forum.djangoproject.com/t/inlineformset-with-a-queryset/22071>\
**Category:** Forms & APIs\
**Created:** [July 4, 2023, 1:23pm UTC](https://forum.djangoproject.com/t/inlineformset-with-a-queryset/22071 "2023-07-04T13:23:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![RichardWoj](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/richardwoj/32/12824_2.png) [@RichardWoj](https://forum.djangoproject.com/u/RichardWoj)\
**Post date:** [July 4, 2023, 1:23pm UTC](https://forum.djangoproject.com/t/inlineformset-with-a-queryset/22071/1 "2023-07-04T13:23:23Z")

</div>

Hello,

I have some troubles with inlineformset while I want to specify the queryset to be used.  
I have the following function:

```auto
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

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [July 4, 2023, 1:29pm UTC](https://forum.djangoproject.com/t/inlineformset-with-a-queryset/22071/2 "2023-07-04T13:29:21Z")

</div>

What do the models look like?

---

<div class="post-metadata">

**Author:** ![RichardWoj](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/richardwoj/32/12824_2.png) [@RichardWoj](https://forum.djangoproject.com/u/RichardWoj)\
**Post date:** [July 5, 2023, 10:00am UTC](https://forum.djangoproject.com/t/inlineformset-with-a-queryset/22071/3 "2023-07-05T10:00:50Z")

</div>

My mistake!  
I have realized that when using framework I did not have company set.  
Fixed
