emmett-framework / emmett

The web framework for inventors
Other
1.04k stars 70 forks source link

belongs_to in User model #307

Closed josejachuf closed 3 years ago

josejachuf commented 3 years ago

Hi,

How can I define a FK field with belongs_to in User model

The problem is that any class (Model) does not exist before User

class Mymodel(Model):
    ...
class User(AuthUser):
    tablename = 'auth_users'
    belongs_to({'mymodel': 'Mymodel'})
auth = Auth(app, db, user_model=User)
db.define_models([Mymodel, User])

I get: AttributeError: 'Database' object has no attribute 'Mymodel'

And if I change the order of definition I get:

db.define_models([Mymodel, User])
auth = Auth(app, db, user_model=User)

AttributeError: 'Database' object has no attribute 'User'

Best Regards Jose

gi0baro commented 3 years ago

@josejachuf you can call define_models how many times you prefer, just define auth models ancestors before, eg:

db.define_models(MyModel)
auth = Auth(app, db, user_model=User)
db.define_models(AnotherModel)

Note: you don't need to define User model on the database since the Auth module takes care of it.

josejachuf commented 3 years ago

Awesome, I didn't know you could do that.

Thanks @gi0baro