mirror of
https://github.com/jazzband/django-eav2.git
synced 2026-04-21 15:34:45 +00:00
Merge pull request #2 from dubizzle/add-custom-update
Add custom update method
This commit is contained in:
commit
83e736e176
2 changed files with 22 additions and 2 deletions
|
|
@ -572,11 +572,11 @@ class Entity(object):
|
|||
for attribute in self.get_all_attributes():
|
||||
if self._hasattr(attribute.slug):
|
||||
attribute_value = self._getattr(attribute.slug)
|
||||
if attribute.datatype == Attribute.TYPE_ENUM and not isinstance(attribute_value, EnumValue):
|
||||
if attribute.datatype == Attribute.TYPE_ENUM and not isinstance(attribute_value, EnumValue) and attribute_value:
|
||||
attribute_value = EnumValue.objects.get(value=attribute_value)
|
||||
if attribute.datatype == Attribute.TYPE_ENUM_MULTI:
|
||||
attribute_value = [
|
||||
EnumValue.objects.get(value=v) if not isinstance(attribute_value, EnumValue) else v
|
||||
EnumValue.objects.get(value=v) if not isinstance(v, EnumValue) else v
|
||||
for v in attribute_value
|
||||
]
|
||||
attribute.save_value(self.instance, attribute_value)
|
||||
|
|
|
|||
|
|
@ -376,3 +376,23 @@ class EavQuerySet(QuerySet):
|
|||
order_clauses.append(term[0])
|
||||
|
||||
return QuerySet.order_by(query_clause, *order_clauses)
|
||||
|
||||
def update(self, **kwargs):
|
||||
config_cls = getattr(self.model, '_eav_config_cls', None)
|
||||
|
||||
prefix = '%s__' % config_cls.eav_attr
|
||||
new_kwargs = {}
|
||||
eav_kwargs = {}
|
||||
|
||||
for key, value in kwargs.items():
|
||||
if key.startswith(prefix):
|
||||
eav_kwargs.update({key[len(prefix):]: value})
|
||||
else:
|
||||
new_kwargs.update({key: value})
|
||||
|
||||
obj = self.first()
|
||||
obj_eav = getattr(obj, config_cls.eav_attr)
|
||||
for key, value in eav_kwargs.items():
|
||||
setattr(obj_eav, key, value)
|
||||
obj.save()
|
||||
return super(EavQuerySet, self).update(**new_kwargs)
|
||||
|
|
|
|||
Loading…
Reference in a new issue