mirror of
https://github.com/jazzband/django-eav2.git
synced 2026-05-23 14:45:55 +00:00
Add multiple optimization to reduce the load on database
This commit is contained in:
parent
36711e036f
commit
074e0b8c4c
3 changed files with 49 additions and 25 deletions
18
eav/migrations/0003_add_values_unique_constraint.py
Normal file
18
eav/migrations/0003_add_values_unique_constraint.py
Normal file
|
|
@ -0,0 +1,18 @@
|
||||||
|
# Generated by Django 3.0.2 on 2020-09-20 11:05
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('contenttypes', '0002_remove_content_type_name'),
|
||||||
|
('eav', '0002_add_new_fields'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AlterUniqueTogether(
|
||||||
|
name='value',
|
||||||
|
unique_together={('entity_ct', 'entity_id', 'attribute_id')},
|
||||||
|
),
|
||||||
|
]
|
||||||
|
|
@ -344,33 +344,28 @@ class Attribute(models.Model):
|
||||||
"""
|
"""
|
||||||
ct = ContentType.objects.get_for_model(entity)
|
ct = ContentType.objects.get_for_model(entity)
|
||||||
|
|
||||||
try:
|
|
||||||
value_obj = self.value_set.get(
|
|
||||||
entity_ct = ct,
|
|
||||||
entity_id = entity.pk,
|
|
||||||
attribute = self
|
|
||||||
)
|
|
||||||
except Value.DoesNotExist:
|
|
||||||
if value in (None, '', []):
|
|
||||||
return
|
|
||||||
|
|
||||||
value_obj = Value.objects.create(
|
|
||||||
entity_ct = ct,
|
|
||||||
entity_id = entity.pk,
|
|
||||||
attribute = self
|
|
||||||
)
|
|
||||||
|
|
||||||
if value in (None, '', []):
|
if value in (None, '', []):
|
||||||
value_obj.delete()
|
self.value_set.filter(
|
||||||
return
|
entity_ct=ct,
|
||||||
|
entity_id=entity.pk,
|
||||||
if value != value_obj.value:
|
).delete()
|
||||||
|
else:
|
||||||
if self.datatype == self.TYPE_ENUM_MULTI:
|
if self.datatype == self.TYPE_ENUM_MULTI:
|
||||||
value_obj.value.clear()
|
value_obj, created = self.value_set.get_or_create(
|
||||||
|
entity_ct=ct,
|
||||||
|
entity_id=entity.pk,
|
||||||
|
)
|
||||||
|
if not created:
|
||||||
|
value_obj.value.clear()
|
||||||
value_obj.value.add(*value)
|
value_obj.value.add(*value)
|
||||||
else:
|
else:
|
||||||
value_obj.value = value
|
value_obj, _ = self.value_set.update_or_create(
|
||||||
value_obj.save()
|
entity_ct=ct,
|
||||||
|
entity_id=entity.pk,
|
||||||
|
defaults={
|
||||||
|
'value_{datatype}'.format(datatype=self.datatype): value,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return '{} ({})'.format(self.name, self.get_datatype_display())
|
return '{} ({})'.format(self.name, self.get_datatype_display())
|
||||||
|
|
@ -398,6 +393,11 @@ class Value(models.Model):
|
||||||
# = <Value: crazy_dev_user - Fav Drink: "red bull">
|
# = <Value: crazy_dev_user - Fav Drink: "red bull">
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
unique_together = [
|
||||||
|
['entity_ct', 'entity_id', 'attribute_id'],
|
||||||
|
]
|
||||||
|
|
||||||
entity_ct = models.ForeignKey(
|
entity_ct = models.ForeignKey(
|
||||||
ContentType,
|
ContentType,
|
||||||
on_delete = models.PROTECT,
|
on_delete = models.PROTECT,
|
||||||
|
|
@ -494,7 +494,8 @@ class Entity(object):
|
||||||
"""
|
"""
|
||||||
instance = kwargs['instance']
|
instance = kwargs['instance']
|
||||||
entity = getattr(kwargs['instance'], instance._eav_config_cls.eav_attr)
|
entity = getattr(kwargs['instance'], instance._eav_config_cls.eav_attr)
|
||||||
entity.validate_attributes()
|
if instance._eav_config_cls.pre_save_validation_enabled:
|
||||||
|
entity.validate_attributes()
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def post_save_handler(sender, *args, **kwargs):
|
def post_save_handler(sender, *args, **kwargs):
|
||||||
|
|
@ -547,7 +548,11 @@ class Entity(object):
|
||||||
Return a query set of all :class:`Attribute` objects that can be set
|
Return a query set of all :class:`Attribute` objects that can be set
|
||||||
for this entity.
|
for this entity.
|
||||||
"""
|
"""
|
||||||
return self.instance._eav_config_cls.get_attributes(self.instance).order_by('display_order')
|
attributes = getattr(self, '_attributes', None)
|
||||||
|
if attributes is None:
|
||||||
|
attributes = self.instance._eav_config_cls.get_attributes(self.instance).order_by('display_order')
|
||||||
|
setattr(self, '_attributes', attributes)
|
||||||
|
return attributes
|
||||||
|
|
||||||
def _hasattr(self, attribute_slug):
|
def _hasattr(self, attribute_slug):
|
||||||
"""
|
"""
|
||||||
|
|
|
||||||
|
|
@ -33,6 +33,7 @@ class EavConfig(object):
|
||||||
eav_attr = 'eav'
|
eav_attr = 'eav'
|
||||||
generic_relation_attr = 'eav_values'
|
generic_relation_attr = 'eav_values'
|
||||||
generic_relation_related_name = None
|
generic_relation_related_name = None
|
||||||
|
pre_save_validation_enabled = True
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def get_attributes(cls, entity=None):
|
def get_attributes(cls, entity=None):
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue