forked from CGM_Public/pretix_original
Optimize number of database queries
This commit is contained in:
@@ -579,7 +579,7 @@ class Item(models.Model):
|
|||||||
self.active = False
|
self.active = False
|
||||||
return super().save()
|
return super().save()
|
||||||
|
|
||||||
def get_all_variations(self):
|
def get_all_variations(self, use_cache=False):
|
||||||
"""
|
"""
|
||||||
This method returns a list containing all variations of this
|
This method returns a list containing all variations of this
|
||||||
item. The list contains one VariationDict per variation, where
|
item. The list contains one VariationDict per variation, where
|
||||||
@@ -590,6 +590,9 @@ class Item(models.Model):
|
|||||||
VariationDicts differ from dicts only by specifying some extra
|
VariationDicts differ from dicts only by specifying some extra
|
||||||
methods.
|
methods.
|
||||||
"""
|
"""
|
||||||
|
if use_cache and hasattr(self, '_get_all_variations_cache'):
|
||||||
|
return self._get_all_variations_cache
|
||||||
|
|
||||||
all_variations = self.variations.all().prefetch_related("values")
|
all_variations = self.variations.all().prefetch_related("values")
|
||||||
all_properties = self.properties.all().prefetch_related("values")
|
all_properties = self.properties.all().prefetch_related("values")
|
||||||
variations_cache = {}
|
variations_cache = {}
|
||||||
@@ -615,6 +618,7 @@ class Item(models.Model):
|
|||||||
var['variation'] = variations_cache[key]
|
var['variation'] = variations_cache[key]
|
||||||
result.append(var)
|
result.append(var)
|
||||||
|
|
||||||
|
self._get_all_variations_cache = result
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ from django.utils.html import format_html
|
|||||||
from django.utils.safestring import mark_safe
|
from django.utils.safestring import mark_safe
|
||||||
from django.utils.translation import ugettext as _
|
from django.utils.translation import ugettext as _
|
||||||
|
|
||||||
from tixlbase.models import ItemVariation, PropertyValue
|
from tixlbase.models import ItemVariation, PropertyValue, Item
|
||||||
|
|
||||||
|
|
||||||
class TolerantFormsetModelForm(forms.ModelForm):
|
class TolerantFormsetModelForm(forms.ModelForm):
|
||||||
@@ -71,8 +71,13 @@ class RestrictionInlineFormset(forms.BaseInlineFormSet):
|
|||||||
render a working empty form for a JavaScript-enabled restriction formset.
|
render a working empty form for a JavaScript-enabled restriction formset.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, data=None, files=None, instance=None,
|
||||||
super().__init__(*args, **kwargs)
|
save_as_new=False, prefix=None, queryset=None, **kwargs):
|
||||||
|
super().__init__(
|
||||||
|
data, files, instance, save_as_new, prefix, queryset, **kwargs
|
||||||
|
)
|
||||||
|
if isinstance(self.instance, Item):
|
||||||
|
self.queryset = self.queryset.prefetch_related("variations")
|
||||||
|
|
||||||
def initialized_empty_form(self):
|
def initialized_empty_form(self):
|
||||||
form = self.form(
|
form = self.form(
|
||||||
@@ -242,7 +247,7 @@ class VariationsField(forms.ModelMultipleChoiceField):
|
|||||||
"""
|
"""
|
||||||
if self.item is None:
|
if self.item is None:
|
||||||
return ()
|
return ()
|
||||||
variations = self.item.get_all_variations()
|
variations = self.item.get_all_variations(use_cache=True)
|
||||||
return (
|
return (
|
||||||
(
|
(
|
||||||
v['variation'].pk if 'variation' in v else v.key(),
|
v['variation'].pk if 'variation' in v else v.key(),
|
||||||
|
|||||||
Reference in New Issue
Block a user