Skip to content

Commit

Permalink
fix django repository fields
Browse files Browse the repository at this point in the history
  • Loading branch information
douwevandermeij committed Apr 21, 2021
1 parent 3c73fc6 commit d867cbd
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
2 changes: 1 addition & 1 deletion fractal/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Fractal is a scaffolding toolkit for building SOLID logic for your Python applications."""

__version__ = "0.1.4"
__version__ = "0.1.5"

from abc import ABC

Expand Down
12 changes: 10 additions & 2 deletions fractal/contrib/django/repositories.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from dataclasses import asdict
from typing import Dict, Generator, Optional, Type

from django.db.models import Model, Q
from django.db.models import Model, Q, ForeignKey

from fractal.contrib.django.specifications import DjangoOrmSpecificationBuilder
from fractal.core.repositories import Entity, Repository
Expand Down Expand Up @@ -30,11 +30,19 @@ def update(self, entity: Entity, upsert=False) -> Entity:
def __get_direct_related_data(self, entity: Entity):
direct_data = {}
related_data = {}

def field_name(field):
if type(field) is ForeignKey:
return field.name + "_id"
return field.name

direct_fields = [field_name(f) for f in self.django_model._meta.fields]
for k, v in asdict(entity).items():
if type(v) == list:
related_data[k] = v
else:
direct_data[k] = v
if k in direct_fields:
direct_data[k] = v
return direct_data, related_data

def __get_obj(self, specification: Specification):
Expand Down

0 comments on commit d867cbd

Please sign in to comment.