Move fields check to register so it happens on server start

This commit is contained in:
Andrew Godwin 2016-07-22 21:40:51 -04:00
parent 174430c817
commit a4c8602ea1
2 changed files with 14 additions and 9 deletions

View File

@ -53,8 +53,15 @@ class Binding(object):
and tie that in as a consumer.
"""
# Model to serialize
model = None
# Only model fields that are listed in fields should be send by default
# if you want to really send all fields, use fields = ['__all__']
fields = None
@classmethod
def register(cls):
"""
@ -66,6 +73,9 @@ class Binding(object):
return
else:
raise ValueError("You must set the model attribute on Binding %r!" % cls)
# If fields is not defined, raise an error
if cls.fields is None:
raise ValueError("You must set the fields attribute on Binding %r!" % cls)
# Optionally resolve model strings
if isinstance(cls.model, six.string_types):
cls.model = apps.get_model(cls.model)

View File

@ -31,11 +31,6 @@ class WebsocketBinding(Binding):
stream = None
# only model fields that are listed in fields should be send by default
# if you want to really send all fields, use fields = ['__all__']
fields = []
# Outbound
@classmethod
def encode(cls, stream, payload):
@ -55,10 +50,10 @@ class WebsocketBinding(Binding):
Serializes model data into JSON-compatible types.
"""
if self.fields == ['__all__']:
self.fields = None
elif not self.fields:
raise ValueError("You must set the fields attribute on Binding %r!" % self.__class__)
data = serializers.serialize('json', [instance], fields=self.fields)
fields = None
else:
fields = self.fields
data = serializers.serialize('json', [instance], fields=fields)
return json.loads(data)[0]['fields']
# Inbound