Django: Add queryset to inlineformsets

谁说我不能喝 提交于 2021-02-20 04:13:12

问题


I want to make a queryset on a field in an inline formset.. I have Inovice and Product models and InvoiceDetails model to link the manytomany relation between them.

here are the models:

class Invoices(models.Model):
    """The Invoice Creation Class."""    
    invoice_number = models.CharField(
        _('invoice_number'), max_length=100, unique=True, null=True)
    ....

class Products(models.Model):
    """Product Creation Class."""

    company = models.ForeignKey(Company, default=1)
    barcode = models.CharField(_('barcode'), max_length=200, null=True)
    ....


class InvoiceDetail(models.Model):
        invoice = models.ForeignKey(Invoices, related_name='parent_invoice')
        product = models.ForeignKey(Products, related_name='parent_product')
        quantity_sold = models.IntegerField(_('quantity_sold'))
        ...

when crearting an invoice i have inline formsets for the products which create an invoice details for every product.. now i want to filter the products that appear for the user to choose from them by the company. i searched a lot on how to override the queryset of inline formsets but found nothing useful for my case.

my forms:

class InvoiceForm(forms.ModelForm):
    class Meta:
        model = Invoices
        fields = ('customer', 'invoice_due_date', 'discount', 'type')

    def __init__(self, *args, **kwargs):
        self.agent = kwargs.pop('agent')
        super(InvoiceForm, self).__init__(*args, **kwargs)

    def clean_customer(self):
      .....

    def clean(self):
      ......



class BaseDetailFormSet(forms.BaseInlineFormSet):

    def clean(self):
         ......


DetailFormset = inlineformset_factory(Invoices,
                                      InvoiceDetail,
                                      fields=('product', 'quantity_sold'),
                                      widgets= {'product': forms.Select(
                                        attrs={
                                            'class': 'search',
                                            'data-live-search': 'true'
                                        })},
                                      formset=BaseDetailFormSet,
                                      extra=1)

and use it in the views like that:

if request.method == 'POST':
        invoice_form = InvoiceForm(
            request.POST, request.FILES, agent=request.user)
        detail_formset = DetailFormset(
            request.POST)
        .......
else:
    invoice_form = InvoiceForm(agent=request.user)
    detail_formset = DetailFormset()

so, how can it filter the products that show in detail_formset by company?


回答1:


I solved it be passing the user to init and loop on forms to override the queryset.

def __init__(self, *args, **kwargs):
    self.agent = kwargs.pop('agent')
    super(BaseDetailFormSet, self).__init__(*args, **kwargs)
    for form in self.forms:
        form.fields['product'].queryset = Products.objects.filter(company=self.agent.company)

in views:

detail_formset = DetailFormset(agent=request.user)


来源:https://stackoverflow.com/questions/48561378/django-add-queryset-to-inlineformsets

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!