redis-om-python
redis-om-python copied to clipboard
Unable to connect
Cannot connect redis_om with django, was able to successfully connect redis-py with django using:
redis.StrictRedis(host='127.0.0.1', port=6379, db=0)
but when tried to connect the redis-om-model with django and fetched the existing data using:
`redis_om_conc = get_redis_connection( url="redis://127.0.0.1:6379/0", decode_responses=True )
class GameList(HashModel): class Meta: database = redis_om_conc
game_id: int
game_name: str
game_code: str
display_platform: int
vendor: str
vendor_code: str
vendor_id: int
languages: str
external_game_id: str
GameList.get("01FVF51D2K8WWTR2BTME97SWZ1")`
it throws an error as:
`NotFoundError Traceback (most recent call last)
Input In [12], in
File ~/Projects/Feeder/cron-env/lib/python3.8/site-packages/redis_om/model/model.py:1324, in HashModel.get(cls, pk) 1322 document = cls.db().hgetall(cls.make_primary_key(pk)) 1323 if not document: -> 1324 raise NotFoundError 1325 try: 1326 result = cls.parse_obj(document)
NotFoundError:`
Getting the Same Problem.
I think, at the moment, it is not possible to use this package in django because django migrations and redis-om migrator are two different things, that's because HashModel
is not a django model to apply migrations. So when you run:
python manage.py makemigrations
python manage.py migrate
It will create an empty migration file. That's why you are getting:
raise NotFoundError
aredis_om.model.model.NotFoundError
Edit;
I tried to do composition, but it didn't work:
from django.db import models
class GameList(models.Model, HashModel):
I also tried to run redis_om Migrator in views or models, didn't work as well:
from redis_om import (
Migrator
)
Migrator().run()
Edit1:
Hey @h-attari, I found a workaround, put your model definition and the migration command in your apps.py
, not in models.py
, file of your Django app because it only runs once:
from redis_om import (
Migrator
)
from redis_om import get_redis_connection, HashModel
redis_om_conc = get_redis_connection(
url="redis://127.0.0.1:6379/0",
decode_responses=True
)
class GameList(HashModel):
game_id: int
game_name: str
game_code: str
display_platform: int
vendor: str
vendor_code: str
vendor_id: int
languages: str
external_game_id: str
class Meta:
database = redis_om_conc
Migrator().run()
Assuming you are using the redis_om
sync version, and not async version aredis_om
.
Note that you don't have to run makemigrations
and migrate
anymore, it is already being managed by redis_om
.