Add custom update method

This commit is contained in:
Shyamala Gopalakrishnan 2020-08-19 11:49:44 +04:00
parent c03101620a
commit cbc59280b8
2 changed files with 21 additions and 1 deletions

View file

@ -572,7 +572,7 @@ 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 = [

View file

@ -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)