Giter VIP home page Giter VIP logo

Comments (6)

DeathBorn avatar DeathBorn commented on August 29, 2024

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.

umutbozkurt avatar umutbozkurt commented on August 29, 2024

Of course, I will implement it this weekend. Or, you can always send a PR

from django-rest-framework-mongoengine.

paslavskyy avatar paslavskyy commented on August 29, 2024

Cool, thank you :).

from django-rest-framework-mongoengine.

umutbozkurt avatar umutbozkurt commented on August 29, 2024

Is this snippet tested?

from django-rest-framework-mongoengine.

umutbozkurt avatar umutbozkurt commented on August 29, 2024

implemented

from django-rest-framework-mongoengine.

DeathBorn avatar DeathBorn commented on August 29, 2024

THanks!

from django-rest-framework-mongoengine.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.