gothinkster / flask-realworld-example-app

Exemplary real world JSON API built with Flask (Python)
https://realworld.io/
MIT License
897 stars 308 forks source link

Why we need to use `Model` instead of `db.Model`? #17

Closed khanh96le closed 5 years ago

khanh96le commented 6 years ago

I just wonder that why when we declare a model, the class should be inherited Model but not db.Model. I checked id(Model), and id(db.Model), both return the same value.

# conduit/user/model.py
from conduit.database import Column, Model, SurrogatePK, db
class User(SurrogatePK, Model):
    __tablename__ = 'users'

Does anyone know the reason?

realazizk commented 6 years ago

Yes beceause Model is the same object as db.Model it's just an alias, I forgot why I did this.

https://github.com/gothinkster/flask-realworld-example-app/blob/master/conduit/database.py#L11

khanh96le commented 6 years ago

But if the Model class inherited from db.Model, it cannot use the functions in CRUDMixin. I think that is the reason you did it.

realazizk commented 6 years ago

@jerry-le yes exactly CRUDMixin is used as a base class for models that's why http://flask-sqlalchemy.pocoo.org/2.3/customizing/