Giter Club home page Giter Club logo

Comments (4)

frol avatar frol commented on May 23, 2024
  1. You might want to start with a DB model field, so you go to teams/models.py and add your field there.

  2. Next, you have several options:
    a. if your DB field can validate everything you need, you just list this field name in the relevant schema (e.g. teams/schemas.py:BaseTeamSchema.Meta.fields).

    class BaseTeamSchema(ModelSchema):
        class Meta:
            fields = (
                Team.id.key,
                Team.title.key,
                Team.my_date.key,
            )
            ...

    b. if you have a custom Schema field (e.g. StringOrDateField), you just define it on the Schema class (e.g. teams/schemas.py:BaseTeamSchema):

    class BaseTeamSchema(ModelSchema):
        my_date = StringOrDateField()
        class Meta:
            fields = (
                Team.id.key,
                Team.title.key,
                'my_date',
            )
            ...

    Alternatively, if you only need to validate the field (meaning that you are not going to change the value, but only check if it is correct in the current context), you can define a validation method on the Schema:

    from marshmallow import validates, ValidationError
    class BaseTeamSchema(ModelSchema):
        ...
        @validates('my_date')
        def validate_my_date(self, value):
            if value != 'now':
                raise ValidationError("My Date must be 'now'.")

    Also, read about validates_schema.

from flask-restplus-server-example.

frol avatar frol commented on May 23, 2024

It would be great to hear from you again about your successes extending this example server!

from flask-restplus-server-example.

joshStillerman avatar joshStillerman commented on May 23, 2024

from flask-restplus-server-example.

frol avatar frol commented on May 23, 2024

There are no views in users. Views are only used in auth, and they are implemented that way because Flask-OAuthlib requires it to be implemented the pure-Flask way. Auth module still needs some improvements, but after the recent refactoring round, it is not as hacky as it used to be, and I believe it is good enough to serve the demo purposes.

Users module is still quite advanced, but it is a completely resources-driven module.

Teams module was created exactly to show how one would implement a nice and clean module, so it is good to start there.

I guess resources (the nouns in the api) are the routes.

Exactly!

api = Namespace('teams', description="Teams")  # pylint: disable=invalid-name


@api.route('/')
class Teams(Resource):
...

Notice the teams argument to the Namespace, that is the common prefix for all of the resources defined in that file using @api.route() decorator.

from flask-restplus-server-example.

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.