You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# To retrieve this customer with its primary key, we use `Customer.get()`:
113
+
assert Customer.get(andrew.pk) == andrew
106
114
```
107
115
108
116
**Ready to learn more?** Check out the [getting started](docs/getting_started.md) guide.
@@ -118,30 +126,45 @@ This validation ensures that fields like `first_name`, which the `Customer` mode
118
126
For example, because we used the `EmailStr` type for the `email` field, we'll get a validation error if we try to create a `Customer` with an invalid email address:
119
127
120
128
```python
121
-
Customer(
122
-
first_name="Andrew",
123
-
last_name="Brookins",
124
-
email="Not an email address!",
125
-
join_date=datetime.date.today(),
126
-
age=38,
127
-
bio="Python developer, works at Redis, Inc."
128
-
)
129
-
```
129
+
import datetime
130
+
from typing import Optional
130
131
131
-
This code generates the following validation error:
132
+
from pydantic import EmailStr, ValidationError
132
133
133
-
```
134
-
Traceback:
135
-
pydantic.error_wrappers.ValidationError: 1 validation error for Customer
136
-
email
137
-
value is not a valid email address (type=value_error.email)
134
+
from redis_om.model import HashModel
135
+
136
+
137
+
classCustomer(HashModel):
138
+
first_name: str
139
+
last_name: str
140
+
email: EmailStr
141
+
join_date: datetime.date
142
+
age: int
143
+
bio: Optional[str]
144
+
145
+
146
+
try:
147
+
Customer(
148
+
first_name="Andrew",
149
+
last_name="Brookins",
150
+
email="Not an email address!",
151
+
join_date=datetime.date.today(),
152
+
age=38,
153
+
bio="Python developer, works at Redis, Inc."
154
+
)
155
+
except ValidationError as e:
156
+
print(e)
157
+
"""
158
+
pydantic.error_wrappers.ValidationError: 1 validation error for Customer
159
+
email
160
+
value is not a valid email address (type=value_error.email)
161
+
"""
138
162
```
139
163
140
164
**Any existing Pydantic validator should work** as a drop-in type annotation with a Redis OM model. You can also write arbitrarily complex custom validations!
141
165
142
166
To learn more, see the [documentation on data validation](docs/validation.md).
143
167
144
-
145
168
## 🔎 Rich Queries and Embedded Models
146
169
147
170
Data modeling, validation, and saving models to Redis all work regardless of how you run Redis.
@@ -157,18 +180,35 @@ Redis OM comes with a rich query language that allows you to query Redis with Py
157
180
To show how this works, we'll make a small change to the `Customer` model we defined earlier. We'll add `Field(index=True)` to tell Redis OM that we want to index the `last_name` and `age` fields:
158
181
159
182
```python
183
+
import datetime
184
+
from typing import Optional
185
+
186
+
from pydantic import EmailStr
187
+
188
+
from redis_om.model import (
189
+
Field,
190
+
HashModel,
191
+
Migrator
192
+
)
193
+
194
+
160
195
classCustomer(HashModel):
161
196
first_name: str
162
197
last_name: str= Field(index=True)
163
198
email: EmailStr
164
199
join_date: datetime.date
165
200
age: int= Field(index=True)
166
201
bio: Optional[str]
167
-
```
202
+
203
+
204
+
# Now, if we use this model with a Redis deployment that has the
205
+
# RediSearch module installed, we can run queries like the following.
168
206
169
-
Now, if we use this model with a Redis deployment that has the [RediSearch module][redisearch-url] installed, we can run queries like the following:
207
+
# Before running queries, we need to run migrations to set up the
208
+
# indexes that Redis OM will use. You can also use the `migrate`
209
+
# CLI tool for this!
210
+
Migrator().run()
170
211
171
-
```python
172
212
# Find all customers with the last name "Brookins"
0 commit comments