Comments (6)
Tried to implement it and it works. You can submit it like string and it will store as binary. No idea how to make binary submitting instead of string...
Add to fields this
from bson import Binary
from mongoengine.python_support import (PY3, bin_type, txt_type, str_types, StringIO)
class BinaryField(serializers.Field):
type_label = 'BinaryField'
def __init__(self, **kwargs):
try:
self.max_bytes = kwargs.pop('max_bytes')
except KeyError:
raise ValueError("BinaryField requires 'max_bytes' kwarg")
super(BinaryField, self).__init__(**kwargs)
def __set__(self, instance, value):
"""Handle bytearrays in python 3.1"""
if PY3 and isinstance(value, bytearray):
value = bin_type(value)
return super(BinaryField, self).__set__(instance, value)
def to_representation(self, value):
return smart_str(value)
def to_internal_value(self, data):
if not isinstance(data, (bin_type, txt_type, Binary)):
raise ValueError("BinaryField only accepts instances of "
"(%s, %s, Binary)" % (bin_type.__name__, txt_type.__name__))
if self.max_bytes is not None and len(data) > self.max_bytes:
raise ValueError('Binary value is too long')
return Binary(str(data)) #str thing somehow should not be here
and edit serializers.py to include mapping me_fields.BinaryField: BinaryField
from django-rest-framework-mongoengine.
Of course, I will implement it this weekend. Or, you can always send a PR
from django-rest-framework-mongoengine.
Cool, thank you :).
from django-rest-framework-mongoengine.
Is this snippet tested?
from django-rest-framework-mongoengine.
implemented
from django-rest-framework-mongoengine.
THanks!
from django-rest-framework-mongoengine.
Related Issues (20)
- Compatible with Django 3.0 HOT 3
- Embedded Document field has no attribute items error in recursive_save
- type object 'XXX' has no attribute '_fields' HOT 2
- Testing API's?
- rest_framework_mongoengine CreateOnlyDefault() not working
- NullBooleanField is deprecated in favor of BooleanField(allow_null=True)
- Serializer Field: `default=0` gets ignored HOT 1
- Let's use ref_name for nested embedded fields as well HOT 5
- The DocumentSerializer uses the default value from the model definition when the field does not even exists in the document yet HOT 2
- Is there a way to speed serializer? HOT 1
- No validation for primary key HOT 1
- `style` not included in `utils.get_field_kwargs()`; not aligned to DRF core arguments HOT 2
- Is nested DynamicEmbeddedDocument of Mongoengine supported in DRF mongoengine? HOT 2
- Is it possible to enable cache? HOT 3
- Create a Permissions Profile HOT 1
- Upon deserializing, reference fields are not present within the validated_data HOT 1
- QuerySet class in MongoEngine does not have .model key as the django.db.models.query.QuerySet.model in
- How using write_concern args when perform_create method is invoke
- Object of type ObjectId is not JSON serializable HOT 2
- AttributeError: module 'rest_framework.fields' has no attribute 'NullBooleanField' HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from django-rest-framework-mongoengine.