Skip to content

Commit

Permalink
Group by table in context
Browse files Browse the repository at this point in the history
  • Loading branch information
timsavage committed Nov 19, 2023
1 parent ee49489 commit 8f53e50
Showing 1 changed file with 23 additions and 14 deletions.
37 changes: 23 additions & 14 deletions oscar_odin/mappings/_model_mapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,17 @@ def __new__(cls, name, bases, attrs):
related_object = getmeta(mapping_type.to_obj).related_objects
for relation in related_object:
if relation.many_to_many:
one_to_one_fields.append(relation.related_name)
one_to_one_fields.append(
(relation.related_name, relation.related_model._meta.db_table)
)
elif relation.many_to_one:
many_to_one_fields.append(relation.related_name)
many_to_one_fields.append(
(relation.related_name, relation.related_model._meta.db_table)
)
elif relation.one_to_many:
many_to_many_fields.append(relation.related_name)
many_to_many_fields.append(
(relation.related_name, relation.related_model._meta.db_table)
)

return mapping_type

Expand All @@ -43,33 +49,36 @@ def create_object(self, **field_values):
"""Create a new product model."""

one_to_one_items = [
(name, field_values.pop(name))
for name in self.one_to_one_fields
(name, table_name, field_values.pop(name))
for name, table_name in self.one_to_one_fields
if name in field_values
]
many_to_one_items = [
(name, field_values.pop(name))
for name in self.many_to_one_fields
(name, table_name, field_values.pop(name))
for name, table_name in self.many_to_one_fields
if name in field_values
]
many_to_many_items = [
(name, field_values.pop(name))
for name in self.many_to_many_fields
(name, table_name, field_values.pop(name))
for name, table_name in self.many_to_many_fields
if name in field_values
]

obj = super().create_object(**field_values)

for name, value in one_to_one_items:
for name, table_name, value in one_to_one_items:
if value:
self.context.setdefault(table_name, []).append(value)
getattr(obj, name).set(value)

for name, value in many_to_one_items:
for name, table_name, value in many_to_one_items:
if value:
getattr(obj, name).set(value)
self.context.setdefault(table_name, []).extend(value)
getattr(obj, name).sadd(*value)

for name, value in many_to_many_items:
for name, table_name, value in many_to_many_items:
if value:
getattr(obj, name).set(value)
self.context.setdefault(table_name, []).extend(value)
getattr(obj, name).add(*value)

return obj

0 comments on commit 8f53e50

Please sign in to comment.