Skip to content

Commit

Permalink
Merge pull request #133 from ChanTsune/feature/refactor
Browse files Browse the repository at this point in the history
Update functions.py
  • Loading branch information
ChanTsune authored Jul 30, 2020
2 parents 7da36df + 6b85582 commit 9051f7b
Showing 1 changed file with 14 additions and 30 deletions.
44 changes: 14 additions & 30 deletions django_boost/utils/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,16 @@ def model_to_json(model, fields=(), exclude=()):
Return value is a dictionary or a list for dictionary.
"""
opts = model._meta
if isinstance(model, models.Model):
json_data = {}

if fields:
for f in fields:
json_data[f] = getattr(model, f)
return json_data

elif exclude:
for f in model._meta.fields:
if f.name not in exclude:
json_data[f.name] = getattr(model, f.name)
return json_data

else:
for f in model._meta.fields:
json_data[f.name] = getattr(model, f.name)
return json_data
for f in opts.fields:
if fields and f.name not in fields:
continue
if exclude and f.name in exclude:
continue
json_data[f.name] = f.value_from_object(model)
return json_data

elif isinstance(model, models.QuerySet):
json_data = [model_to_json(m, fields, exclude) for m in model]
Expand All @@ -36,20 +28,12 @@ def model_to_json(model, fields=(), exclude=()):

def json_to_model(model_class, dic, fields=(), exclude=()):
model = model_class()
if fields:
for key, value in dic.items():
if key in fields:
setattr(model, key, value)
return model

elif exclude:
for key, value in dic.items():
if key not in exclude:
setattr(model, key, value)
return model

for key, value in dic.items():
setattr(model, key, value)
for f in model._meta.fields:
if fields and f.name not in fields:
continue
if exclude and f.name in exclude:
continue
setattr(model, f.name, dic[f.name])
return model


Expand Down

0 comments on commit 9051f7b

Please sign in to comment.