Delete
Pydantic-redis can be used to delete model instances from redis.
Create and register the Model
A model is a class that inherits from Model
with its _primary_key_field
attribute set.
In order for the store to know the existence of the given model,
register it using the register_model
method of Store
import pprint
from pydantic_redis import Model, Store, RedisConfig
class Book(Model):
_primary_key_field: str = "title"
title: str
author: str
if __name__ == "__main__":
pp = pprint.PrettyPrinter(indent=4)
store = Store(
name="some_name", redis_config=RedisConfig(), life_span_in_seconds=86400
)
store.register_model(Book)
Book.insert(
[
Book(title="Oliver Twist", author="Charles Dickens"),
Book(title="Jane Eyre", author="Emily Bronte"),
Book(title="Pride and Prejudice", author="Jane Austen"),
Book(title="Utah Blaine", author="Louis L'Amour"),
]
)
pre_delete_response = Book.select()
Book.delete(ids=["Oliver Twist", "Pride and Prejudice"])
post_delete_response = Book.select()
print("pre-delete:")
pp.pprint(pre_delete_response)
print("\npost-delete:")
pp.pprint(post_delete_response)
Delete Records
To delete many records from redis, pass a list of primary keys (ids
) of the records to the model's delete
method.
import pprint
from pydantic_redis import Model, Store, RedisConfig
class Book(Model):
_primary_key_field: str = "title"
title: str
author: str
if __name__ == "__main__":
pp = pprint.PrettyPrinter(indent=4)
store = Store(
name="some_name", redis_config=RedisConfig(), life_span_in_seconds=86400
)
store.register_model(Book)
Book.insert(
[
Book(title="Oliver Twist", author="Charles Dickens"),
Book(title="Jane Eyre", author="Emily Bronte"),
Book(title="Pride and Prejudice", author="Jane Austen"),
Book(title="Utah Blaine", author="Louis L'Amour"),
]
)
pre_delete_response = Book.select()
Book.delete(ids=["Oliver Twist", "Pride and Prejudice"])
post_delete_response = Book.select()
print("pre-delete:")
pp.pprint(pre_delete_response)
print("\npost-delete:")
pp.pprint(post_delete_response)
Run the App
Running the above code in a file main.py
would produce:
Tip
Probably FLUSHALL redis first
$ python main.py
pre-delete:
[ Book(title='Jane Eyre', author='Emily Bronte'),
Book(title='Oliver Twist', author='Charles Dickens'),
Book(title='Utah Blaine', author="Louis L'Amour"),
Book(title='Pride and Prejudice', author='Jane Austen')]
post-delete:
[ Book(title='Jane Eyre', author='Emily Bronte'),
Book(title='Utah Blaine', author="Louis L'Amour")]