Skip to content

Commit

Permalink
DRF 3.11 compat
Browse files Browse the repository at this point in the history
  • Loading branch information
sheppard committed Jan 10, 2020
1 parent 28551ff commit b0b0c0e
Showing 1 changed file with 26 additions and 8 deletions.
34 changes: 26 additions & 8 deletions natural_keys/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,30 @@

class NaturalKeyValidator(serializers.UniqueTogetherValidator):
def set_context(self, serializer):
self.nested_fields = {
if getattr(self, 'requires_context', None):
# DRF 3.11+
pass
else:
# DRF 3.10 and older
self.serializer = serializer
super(NaturalKeyValidator, self).set_context(serializer)

def filter_queryset(self, attrs, queryset, serializer=None):
if not serializer:
# DRF 3.10 and older
serializer = self.serializer

nested_fields = {
name: serializer.fields[name]
for name in self.fields
if isinstance(serializer.fields[name], NaturalKeySerializer)
}
super(NaturalKeyValidator, self).set_context(serializer)

def filter_queryset(self, attrs, queryset):
attrs = attrs.copy()
for field in attrs:
if field in self.nested_fields:
if field in nested_fields:
assert(isinstance(attrs[field], dict))
cls = self.nested_fields[field].Meta.model
cls = nested_fields[field].Meta.model
result = cls._default_manager.filter(
**attrs[field]
)
Expand All @@ -31,9 +42,16 @@ def filter_queryset(self, attrs, queryset):
# Existing nested object, use it to validate
attrs[field] = result[0].pk

return super(NaturalKeyValidator, self).filter_queryset(
attrs, queryset
)
if getattr(self, 'requires_context', None):
# DRF 3.11+
return super(NaturalKeyValidator, self).filter_queryset(
attrs, queryset, serializer
)
else:
# DRF 3.10 and older
return super(NaturalKeyValidator, self).filter_queryset(
attrs, queryset
)


class NaturalKeySerializer(JSONFormModelSerializer):
Expand Down

0 comments on commit b0b0c0e

Please sign in to comment.