gpt4 book ai didi

Django modelformset_factory() 过滤

转载 作者:行者123 更新时间:2023-12-04 17:50:39 29 4
gpt4 key购买 nike

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

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

有效但速度很慢的代码是:
formset = modelformset_factory(Transaction, form=PaidDateForm, extra=0, can_delete=False)

此外,表单集:
formset = formset(request.POST, Transaction.objects.filter(pk__in=qs))

我想要过滤的 QueryString 称为“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)

最佳答案

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

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

第一的:

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

第二个选项是创建 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)

这段代码对你有帮助吗?

关于Django modelformset_factory() 过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17197194/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com