问题描述
请帮助我,在使用ModelChoiceField
时如何减少数据库调用,因为它需要一个queryset
,并且我必须将其与递归外键一起使用,分别使用三次,代码如下:
self.fields['category'] = forms.ModelChoiceField(queryset=queryset)
self.fields['super_category'] = forms.ModelChoiceField(queryset=)
self.fields['product_type'] = forms.ModelChoiceField(queryset=)
模型类:
class Category(ProjectBaseModel,AuditLogMixin):
parent_id = models.ForeignKey('self',related_name='children',blank=True,null=True,on_delete=models.CASCADE,verbose_name=_('Parent'))
我试图做的是在数组中收集所需类别的所有ID,并仅对它们进行一个过滤查询集,如下所示:
category = auction.category
super_category = category.parent_id
product_type = super_category.parent_id
ids= [category.id,super_category.id,product_type.id]
queryset = Category.objects.filter(id__in=ids)
如何继续该解决方案
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)