Coder Social home page Coder Social logo

Comments (7)

sloria avatar sloria commented on August 19, 2024

You could do something like:

from marshmallow import fields, Schema, ValidationError

def read_only(val):
    if val is not fields.missing:
        raise ValidationError('Cannot pass a read-only field.')


class AlbumSchema(Schema):
    published = fields.Date(validate=read_only)
    title = fields.Str()


s = AlbumSchema()
result = s.load({'published': '2014-11-18'})
print(result.errors)  # {'published': ['Cannot pass a read-only field.']}

from marshmallow.

pmdarrow avatar pmdarrow commented on August 19, 2024

This works, but what do you think about making readonly an optional keyword argument, just like required is? Django REST Framework, Cerberus and a number of other serilzation / validation libraries implement it this way and I personally find it very convinient.

from marshmallow.

rozenm avatar rozenm commented on August 19, 2024

I looked for a read only attribute as well.
Used to working with Django Rest FrameWork
and the read only flag is quite handy.

from marshmallow.

sloria avatar sloria commented on August 19, 2024

I will consider adding a read-only flag as a kwarg. Thank you for your input.

from marshmallow.

philtay avatar philtay commented on August 19, 2024

+1

from marshmallow.

sloria avatar sloria commented on August 19, 2024

I am leaning towards adding this feature, but I will probably call the parameter dump_only or something similar, for consistency with load_only as described in #87. This would also avoid confusion over what "read" and "write" mean (keep in mind that marshmallow isn't meant to do any sort of "writing").

Thoughts?

from marshmallow.

sloria avatar sloria commented on August 19, 2024

Added in #131

from marshmallow.

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.