2👍
Ok after many searches and several fail tries.
I finally managed to remove all duplicated/similar queries by using Model form and adding it to admin inline
class OrderItemForm(forms.ModelForm):
""" OrderItem Model Form """
products_choices = [('', '---------')]
for item in Product.objects.values('pk', 'name', 'arabic_name'):
products_choices.append(
(item['pk'], f"{item['arabic_name']}-{item['name']}"))
product = forms.ChoiceField(required=True,
choices=products_choices)
variants_choices = [('', '---------')]
variants_choices.extend(Variants.objects.values_list('pk', 'name'))
variant = forms.ChoiceField(required=False,
choices=variants_choices)
class OrderItemStackAdmin(admin.TabularInline):
""" Stacked Order Items """
model = OrderItem
form = OrderItemForm
autocomplete_fields = ['product_add_ons']
extra = 0
fieldsets = (
(_('Product'), {
'fields': ('product', 'variant')
}), (_('Add-Ons'), {
'fields': ('product_add_ons', 'add_ons_note')
}), (_('Price'), {
'fields': ('quantity', 'product_price', 'add_ons_total_price')
}), (_('Discount'), {
'fields': ('discount',)
})
)
def get_queryset(self, request):
qs = super().get_queryset(request)
return qs.prefetch_related('product_add_ons')
Source:stackexchange.com