Fixes serializer handling default dataclass fields

This commit is contained in:
Mouse Reeve
2020-11-25 11:15:14 -08:00
parent 9b57cfd331
commit aed360d07e
2 changed files with 19 additions and 7 deletions

View File

@ -74,7 +74,8 @@ class ActivityObject:
try:
value = kwargs[field.name]
except KeyError:
if field.default == MISSING:
if field.default == MISSING and \
field.default_factory == MISSING:
raise ActivitySerializerError(\
'Missing required field: %s' % field.name)
value = field.default
@ -143,6 +144,8 @@ class ActivityObject:
# add images
for (model_key, value) in image_fields.items():
if not value:
continue
getattr(instance, model_key).save(*value, save=True)
# add one to many fields
@ -188,6 +191,8 @@ def resolve_foreign_key(model, remote_id):
def tag_formatter(tags, tag_type):
''' helper function to extract foreign keys from tag activity json '''
if not isinstance(tags, list):
return []
items = []
types = {
'Book': models.Book,
@ -207,9 +212,9 @@ def tag_formatter(tags, tag_type):
def image_formatter(image_json):
''' helper function to load images and format them for a model '''
url = image_json.get('url')
if not url:
if not image_json or not hasattr(image_json, 'url'):
return None
url = image_json.get('url')
try:
response = requests.get(url)