def create(self, validated_data): raise_errors_on_nested_writes('create', self, validated_data) ModelClass = self.Meta.model try: # recursively create EmbeddedDocuments from their validated data # before creating the document instance itself instance = self.recursive_save(validated_data) except TypeError as exc: msg = ('Got a `TypeError` when calling `%s.objects.create()`. ' 'This may be because you have a writable field on the ' 'serializer class that is not a valid argument to ' '`%s.objects.create()`. You may need to make the field ' 'read-only, or override the %s.create() method to handle ' 'this correctly.\nOriginal exception text was: %s.' % (ModelClass.__name__, ModelClass.__name__, type(self).__name__, exc)) raise TypeError(msg) except me_ValidationError as exc: msg = ( 'Got a `ValidationError` when calling `%s.objects.create()`. ' 'This may be because request data satisfies serializer validations ' 'but not Mongoengine`s. You may need to check consistency between ' '%s and %s.\nIf that is not the case, please open a ticket ' 'regarding this issue on https://github.com/umutbozkurt/django-rest-framework-mongoengine/issues' '\nOriginal exception was: %s' % (ModelClass.__name__, ModelClass.__name__, type(self).__name__, exc)) raise me_ValidationError(msg) return instance
def create(self, validated_data): """ Create an instance using queryset.create() Before create() on self, call EmbeddedDocumentSerializer's create() first. If exists. """ raise_errors_on_nested_writes('create', self, validated_data) # Automagically create and set embedded documents to validated data for name in self.field_info.embedded.keys(): field = self.fields[name] embedded_doc_intance = field.create(field.validated_data) validated_data[name] = embedded_doc_intance ModelClass = self.Meta.model try: instance = ModelClass(**validated_data) instance.save() except TypeError as exc: msg = ( 'Got a `TypeError` when calling `%s.objects.create()`. ' 'This may be because you have a writable field on the ' 'serializer class that is not a valid argument to ' '`%s.objects.create()`. You may need to make the field ' 'read-only, or override the %s.create() method to handle ' 'this correctly.\nOriginal exception text was: %s.' % ( ModelClass.__name__, ModelClass.__name__, type(self).__name__, exc ) ) raise TypeError(msg) except me_ValidationError as exc: msg = ( 'Got a `ValidationError` when calling `%s.objects.create()`. ' 'This may be because request data satisfies serializer validations ' 'but not Mongoengine`s. You may need to check consistency between ' '%s and %s.\nIf that is not the case, please open a ticket ' 'regarding this issue on https://github.com/umutbozkurt/django-rest-framework-mongoengine/issues' '\nOriginal exception was: %s' % ( ModelClass.__name__, ModelClass.__name__, type(self).__name__, exc ) ) raise me_ValidationError(msg) return instance
def create(self, validated_data): """ Create an instance using queryset.create() Before create() on self, call EmbeddedDocumentSerializer's create() first. If exists. """ raise_errors_on_nested_writes('create', self, validated_data) # Automagically create and set embedded documents to validated data for embedded_field in self.embedded_document_serializer_fields: embedded_doc_intance = embedded_field.create(embedded_field.validated_data) validated_data[embedded_field.field_name] = embedded_doc_intance ModelClass = self.Meta.model try: instance = ModelClass(**validated_data) instance.save() except TypeError as exc: msg = ( 'Got a `TypeError` when calling `%s.objects.create()`. ' 'This may be because you have a writable field on the ' 'serializer class that is not a valid argument to ' '`%s.objects.create()`. You may need to make the field ' 'read-only, or override the %s.create() method to handle ' 'this correctly.\nOriginal exception text was: %s.' % ( ModelClass.__name__, ModelClass.__name__, type(self).__name__, exc ) ) raise TypeError(msg) except me_ValidationError as exc: msg = ( 'Got a `ValidationError` when calling `%s.objects.create()`. ' 'This may be because request data satisfies serializer validations ' 'but not Mongoengine`s. You may need to check consistency between ' '%s and %s.\nIf that is not the case, please open a ticket ' 'regarding this issue on https://github.com/umutbozkurt/django-rest-framework-mongoengine/issues' '\nOriginal exception was: %s' % ( ModelClass.__name__, ModelClass.__name__, type(self).__name__, exc ) ) raise me_ValidationError(msg) return instance
def create(self, validated_data): raise_errors_on_nested_writes('create', self, validated_data) ModelClass = self.Meta.model try: # recursively create EmbeddedDocuments from their validated data # before creating the document instance itself instance = self.recursive_create(validated_data) except TypeError as exc: msg = ( 'Got a `TypeError` when calling `%s.objects.create()`. ' 'This may be because you have a writable field on the ' 'serializer class that is not a valid argument to ' '`%s.objects.create()`. You may need to make the field ' 'read-only, or override the %s.create() method to handle ' 'this correctly.\nOriginal exception text was: %s.' % ( ModelClass.__name__, ModelClass.__name__, type(self).__name__, exc ) ) raise TypeError(msg) except me_ValidationError as exc: msg = ( 'Got a `ValidationError` when calling `%s.objects.create()`. ' 'This may be because request data satisfies serializer validations ' 'but not Mongoengine`s. You may need to check consistency between ' '%s and %s.\nIf that is not the case, please open a ticket ' 'regarding this issue on https://github.com/umutbozkurt/django-rest-framework-mongoengine/issues' '\nOriginal exception was: %s' % ( ModelClass.__name__, ModelClass.__name__, type(self).__name__, exc ) ) raise me_ValidationError(msg) return instance