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