Add some model link to existing model

I created a module that serves my own authentication system and users. This is a universal module that I use in different applications. This module describes the model User, for example:

class User(db.Model):
    email = db.EmailProperty()
    password = db.StringProperty()
    role = db.StringProperty(default=roles.USER)

In any application in which I use this module, I would like to create an additional model that describes additional fields specific to this application, like this:

 class UserProfile(db.Model):
        first_name = db.StringProperty()
        last_name = db.StringProperty()
        company = db.StringProperty()

And I need to attach the model UserProfileto the model Userinside my new application. How can I do this without having to change the code inside the module, which is common to all applications?

+3
source share
3 answers

, , , .

, baseuser.py, :

class UserModel(db.Model):
    email = db.EmailProperty()
    password = db.StringProperty()
    role = db.StringProperty(default=roles.USER)

GAE :

from baseuser import UserModel

class User(UserModel):
    first_name = db.StringProperty()
    last_name = db.StringProperty()
    company = db.StringProperty()

.

+2

, , -, " ". , . UserProfile :

class UserProfile(db.Model):
        user = db.ReferenceProperty(User)
        first_name = db.StringProperty()
        last_name = db.StringProperty()
        company = db.StringProperty()

:

user = User.filter('email =', email).get()
profile = UserProfile.filter('user =', user).get()

, , User, , , :

class User(db.Model):
    email = db.EmailProperty()
    password = db.StringProperty()
    role = db.StringProperty(default=roles.USER)
    profile = db.ReferenceProperty()

. , , . .

, User Expando.

+1

? UserProfile , . User UserProfile (UserProfile ) user UserProfile , UserProfile .

, , . :

class User(db.Model):
    email = db.EmailProperty()
    password = db.StringProperty()
    role = db.StringProperty(default=roles.USER)
    if APP_HAS_USER_PROFILES:
        first_name = db.StringProperty()
        last_name = db.StringProperty()
        company = db.StringProperty()

Then these last three fields will be displayed only in certain applications. I believe that you can also inherit UserProfile from the user, but I'm not sure what will happen then (from the point of view of the database).

0
source

All Articles