Refactor
Moves from_json to utils and splits the models to files in a module
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
def from_json(cls, json):
|
||||
try:
|
||||
return cls.objects.get(pk=json["id"])
|
||||
except cls.DoesNotExist:
|
||||
keys = [f.name for f in cls._meta.get_fields()]
|
||||
subset = {key:json[key] for key in set(keys) & set(json.keys())}
|
||||
return cls.objects.create(**subset)
|
||||
|
||||
Reference in New Issue
Block a user