redis / redis-om-python

Object mapping, and more, for Redis and Python
MIT License
1.07k stars 108 forks source link

How to Properly Inheret Base and set Model Key Prefix #509

Open thisiseddy-ab opened 1 year ago

thisiseddy-ab commented 1 year ago

I've Seen this video NoSQL Data Modeling with Redis: Building 1-to-Many Relationships , he inherits the base and sets the model key prefix.But he never shows the base code i have no idea how he does it.

This is with what i cam up

def Base(model_prefix):
    class BaseModel():
        class Meta:
            global_key_prefix = RPU_REDIS_DATA_PREFIX
            model_key_prefix = model_prefix
            database =  get_redis_connection(host=RPU_REDIS_DATA_HOST,port=RPU_REDIS_DATA_PORT,db=RPU_REDIS_DATA_DB,decode_responses=True)
    return BaseModel

class User(Base("User"),JsonModel):
    My_Id : Optional[str] = Field(primary_key=True,index=True,default=str(ULID()))
    username: str = Field(index=True)
    name: str
    lastname: str

I don't know is this right how to write the BASE and if it creates a connection for every model this will be bad ?