Django modelformset_factory()过滤 [英] Django modelformset_factory() filtering

查看:132
本文介绍了Django modelformset_factory()过滤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从查询中过滤出大量的对象。目前,它正在抓取类中的所有对象,我想将其过滤到查询字符串中的相关对象。我该怎么做?当我尝试时,我得到一个属性错误,说明

 ''QuerySet'对象没有属性'__name__''

代码的工作原理很慢,但是很简单:

  formset = modelformset_factory(Transaction,form = PaidDateForm,extra = 0,can_delete = False)

另外,formset:

  formset = formset(request.POST,Transaction.objects.filter pk__in = qs))

我想要过滤的QueryString称为qs / p>

  class PaidDateForm(forms.ModelForm):
formfield_callback = jquery_datefield
金额,max_digits = 14,decimal_places = 2,required = False)
date_cleared = forms.DateField(label =清除日期,widget = JQueryDateWidget(),input_formats = settings.DATE_INPUT_FORMATS,required = False)

class Meta :
model = Transaction
include =('date_time_created ')

def __init __(self,* args,** kwargs):
self.queryset = Transaction.objects.filter(pk__in = qs)
super(PaidDateForm,self ).__ init __(* args,** kwargs)
for self.fields中的字段:
如果field!='date_cleared':
self.fields [field] .widget = forms.HiddenInput ()
self.fields ['paid_amount']。widget.attrs ['size'] = 12
self.initial ['paid_amount'] ='%.2f'%(self.instance.usd_amount )


解决方案

查看Django文档中的示例:
https://docs.djangoproject.com/ en / dev / topics / forms / modelforms /#changing-the-queryset



如果我正确理解您的问题,您的问题有两种方法: / p>

首先:

  TransactionFormset = mode lformset_factory(Transaction,form = PaidDateForm,extra = 0,can_delete = False)
formset = TransactionFormset(queryset = Transaction.objects.filter(pk__in = qs))
pre>

第二个选项是创建BaseTransactionFormset

  class BaseTransactionFormSet(BaseModelFormSet )
def __init __(self,* args,** kwargs):
super(BaseTransactionFormSet,self).__ init __(* args,** kwargs)

#create过滤这里任何适合你需要
self.queryset = Transaction.objects.filter()

formset = modelformset_factory(Transaction,formset = BaseTransactionFormSet,form = PaidDateForm,extra = 0,can_delete = False )

此代码是否帮助您?


I'm needing to filter out a significant amount of objects from my query. Currently, it is grabbing all objects in the class, and I want to filter it to the relevant ones which are in a querystring. How can I do this? When I try, I get an Attribute Error stating

''QuerySet' object has no attribute '__name__'.'

The code that works, but very slowly is:

formset = modelformset_factory(Transaction, form=PaidDateForm, extra=0, can_delete=False)

Also, the formset:

formset = formset(request.POST, Transaction.objects.filter(pk__in=qs))

The QueryString that I am wanting to filter by is called 'qs.'

class PaidDateForm(forms.ModelForm):
    formfield_callback = jquery_datefield
Amount",max_digits=14,decimal_places=2,required=False)
    date_cleared = forms.DateField(label="Cleared Date",widget=JQueryDateWidget(), input_formats=settings.DATE_INPUT_FORMATS, required=False)

    class Meta:
        model = Transaction
        include = ('date_time_created')

    def __init__(self, *args, **kwargs):
        self.queryset = Transaction.objects.filter(pk__in=qs)
        super(PaidDateForm, self).__init__(*args, **kwargs)
        for field in self.fields:
            if field != 'date_cleared':
                self.fields[field].widget = forms.HiddenInput()
        self.fields['paid_amount'].widget.attrs['size'] = 12
        self.initial['paid_amount'] = '%.2f' % (self.instance.usd_amount)

解决方案

Look at the example in Django documentation: https://docs.djangoproject.com/en/dev/topics/forms/modelforms/#changing-the-queryset

If I understand your question correctly there is two approach to your problem:

First:

TransactionFormset = modelformset_factory(Transaction,form=PaidDateForm, extra=0, can_delete=False)
formset = TransactionFormset(queryset=Transaction.objects.filter(pk__in=qs))

Second options is to create BaseTransactionFormset

class BaseTransactionFormSet(BaseModelFormSet):
    def __init__(self, *args, **kwargs):
        super(BaseTransactionFormSet, self).__init__(*args, **kwargs)

        #create filtering here whatever that suits you needs
        self.queryset = Transaction.objects.filter()

formset = modelformset_factory(Transaction, formset=BaseTransactionFormSet,form=PaidDateForm, extra=0, can_delete=False)

Does this code help you?

这篇关于Django modelformset_factory()过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆