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. and tie that in as a consumer.
""" """
# Model to serialize
model = None 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 @classmethod
def register(cls): def register(cls):
""" """
@ -66,6 +73,9 @@ class Binding(object):
return return
else: else:
raise ValueError("You must set the model attribute on Binding %r!" % cls) 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 # Optionally resolve model strings
if isinstance(cls.model, six.string_types): if isinstance(cls.model, six.string_types):
cls.model = apps.get_model(cls.model) cls.model = apps.get_model(cls.model)

View File

@ -31,11 +31,6 @@ class WebsocketBinding(Binding):
stream = None 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 # Outbound
@classmethod @classmethod
def encode(cls, stream, payload): def encode(cls, stream, payload):
@ -55,10 +50,10 @@ class WebsocketBinding(Binding):
Serializes model data into JSON-compatible types. Serializes model data into JSON-compatible types.
""" """
if self.fields == ['__all__']: if self.fields == ['__all__']:
self.fields = None fields = None
elif not self.fields: else:
raise ValueError("You must set the fields attribute on Binding %r!" % self.__class__) fields = self.fields
data = serializers.serialize('json', [instance], fields=self.fields) data = serializers.serialize('json', [instance], fields=fields)
return json.loads(data)[0]['fields'] return json.loads(data)[0]['fields']
# Inbound # Inbound