-
-
Notifications
You must be signed in to change notification settings - Fork 59
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(entity): optimize entity save method and add bulk save functionality #663
base: master
Are you sure you want to change the base?
Changes from all commits
127bae2
6a80a3a
2fddeab
29ceb87
e3ae5b5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -99,9 +99,11 @@ def _getattr(self, attribute_slug): | |
""" | ||
return self.__dict__[attribute_slug] | ||
|
||
def save(self): | ||
def save(self, *, commit=True): | ||
"""Saves all the EAV values that have been set on this entity.""" | ||
for attribute in self.get_all_attributes(): | ||
values = {} | ||
attributes = self.get_all_attributes() | ||
for attribute in attributes: | ||
if self._hasattr(attribute.slug): | ||
attribute_value = self._getattr(attribute.slug) | ||
if ( | ||
|
@@ -113,7 +115,12 @@ def save(self): | |
and attribute_value is not None | ||
): | ||
attribute_value = EnumValue.objects.get(value=attribute_value) | ||
attribute.save_value(self.instance, attribute_value) | ||
if commit: | ||
attribute.save_value(self.instance, attribute_value) | ||
values[attribute.slug] = attribute_value | ||
if not commit: | ||
attributes_value = self.save_bulk(values, attributes) | ||
Value.objects.bulk_create(attributes_value) | ||
|
||
def validate_attributes(self): | ||
""" | ||
|
@@ -200,6 +207,50 @@ def __iter__(self): | |
""" | ||
return iter(self.get_values()) | ||
|
||
def save_bulk( | ||
self, | ||
eav_values, | ||
attributes, | ||
): | ||
Comment on lines
+210
to
+214
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In line with the other comment, should Thinking of how Django's
Note I did a generic Also, thinking about it, we should use the same language as Django with "bulk_update" and their params to not have confusion around things like expecting signals to be called like a typical What do you think? |
||
""" | ||
Prepare a list of EAV Value objects for bulk creation. | ||
|
||
This method takes a dictionary of EAV attribute values and a list of Attribute | ||
objects, and returns a list of Value objects that can be bulk created to update | ||
the EAV data for the current Entity instance. | ||
|
||
Args: | ||
eav_values: A dictionary mapping attribute slugs to their new values. | ||
attributes: A list of Attribute objects associated with the current Entity. | ||
|
||
Returns: | ||
A list of Value objects that can be bulk created. | ||
""" | ||
eav_values_to_create = [] | ||
if not eav_values: | ||
return eav_values_to_create | ||
|
||
ct = ContentType.objects.get_for_model(self.instance) | ||
attribute_slugs = list(eav_values.keys()) | ||
|
||
for attr_slug in attribute_slugs: | ||
entity_data = { | ||
"entity_ct": ct, | ||
"attribute": next( | ||
( | ||
attribute | ||
for attribute in attributes | ||
if attribute.slug == attr_slug | ||
), | ||
None, | ||
), | ||
get_entity_pk_type(self): self.pk, | ||
"value": eav_values[attr_slug], | ||
} | ||
eav_values_to_create.append(Value(**entity_data)) | ||
|
||
return eav_values_to_create | ||
|
||
|
||
class EAVModelMeta(ModelBase): | ||
def __new__(cls, name, bases, namespace, **kwds): | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If
commit=False
, I would expect nothing to be saved to the database, which is line with Django behavior. Instead, what do you think about returning alist[Value]
back? This would work for any number of attributes that are updated during aneav.save()
call.