Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Use proper subtyped

Patryk Zawadzki2ab858f89123
diff --git a/product/models.py b/product/models.py
index 5aa876e60c..0e9f1e133e 100644
--- a/product/models.py
+++ b/product/models.py
@@ -1,5 +1,6 @@
from decimal import Decimal
from django.db import models
+from django.db.models.query import QuerySet
from django.db.models.fields.related import SingleRelatedObjectDescriptor
from django.utils.safestring import mark_safe
from django.utils.translation import pgettext_lazy as _
@@ -10,7 +11,7 @@ from unidecode import unidecode
import re
-class SubtypedManager(models.Manager):
+class SubtypedQuerySet(QuerySet):
def find_subclasses(self, root):
for a in dir(root):
@@ -23,62 +24,50 @@ class SubtypedManager(models.Manager):
for s in self.find_subclasses(child):
yield '%s__%s' % (a, s)
- # https://code.djangoproject.com/ticket/16572
- #def get_query_set(self):
- # qs = super(SubtypedManager, self).get_query_set()
- # subclasses = list(self.find_subclasses(self.model))
- # if subclasses:
- # return qs.select_related(*subclasses)
- # return qs
+ def subcast(self, obj):
+ subtype = obj
+ while True:
+ root = type(subtype)
+ last_root = root
+ for a in dir(root):
+ attr = getattr(root, a)
+ if isinstance(attr, SingleRelatedObjectDescriptor):
+ child = attr.related.model
+ if (issubclass(child, root) and
+ child is not root):
+ next = getattr(subtype, a)
+ if next:
+ subtype = next
+ break
+ if root == last_root:
+ break
+ return subtype
+ def iterator(self, filter=True):
+ subclasses = list(self.find_subclasses(self.model))
+ if subclasses and filter:
+ # https://code.djangoproject.com/ticket/16572
+ for obj in self.select_related(*subclasses).iterator(filter=False):
+ yield obj
+ else:
+ objs = super(SubtypedQuerySet, self).iterator()
+ for obj in objs:
+ yield self.subcast(obj)
-class Subtyped(models.Model):
- subtype_attr = models.CharField(max_length=500, editable=False)
- __in_unicode = False
+class SubtypedManager(models.Manager):
+
+ def get_query_set(self):
+ return SubtypedQuerySet(self.model)
+
+
+class Subtyped(models.Model):
objects = SubtypedManager()
class Meta:
abstract = True
- def __unicode__(self):
- # XXX: can we do it in more clean way?
- if self.__in_unicode:
- return unicode(super(Subtyped, self))
- subtype_instance = self.get_subtype_instance()
- if type(subtype_instance) is type(self):
- self.__in_unicode = True
- res = self.__unicode__()
- self.__in_unicode = False
- return res
- return subtype_instance.__unicode__()
-
- def get_subtype_instance(self):
- """
- Caches and returns the final subtype instance. If refresh is set,
- the instance is taken from database, no matter if cached copy
- exists.
- """
- subtype = self
- path = self.subtype_attr.split()
- whoami = self._meta.module_name
- remaining = path[path.index(whoami) + 1:]
- for r in remaining:
- subtype = getattr(subtype, r)
- return subtype
-
- def store_subtype(self, klass):
- if not self.id:
- path = [self]
- parents = self._meta.parents.keys()
- while parents:
- parent = parents[0]
- path.append(parent)
- parents = parent._meta.parents.keys()
- path = [p._meta.module_name for p in reversed(path)]
- self.subtype_attr = ' '.join(path)
-
class Category(MPTTModel):
@@ -102,7 +91,7 @@ class Product(Subtyped, Item):
max_digits=12, decimal_places=4)
category = models.ForeignKey(Category, related_name='products',
verbose_name=_('Product field', 'category'))
- stock = models.DecimalField(_('Product item field','stock'),
+ stock = models.DecimalField(_('Product item field', 'stock'),
max_digits=10, decimal_places=4,
default=Decimal(1))