popoto.fields.auto_field_mixin¶
popoto.fields.auto_field_mixin
¶
Auto Field Mixin for Popoto Redis ORM.
This module provides automatic ID generation for models that need guaranteed uniqueness without requiring the developer to manage identifiers.
Design Philosophy:¶
In Redis, every stored object needs a unique key. Popoto's approach mirrors relational database patterns where you might have a natural composite key (like country + city) or a synthetic auto-incrementing ID. The AutoFieldMixin solves the "synthetic ID" case for Redis.
The key insight is that UUID4 provides probabilistically unique identifiers without requiring coordination (unlike auto-increment, which needs a central counter). This makes it ideal for distributed systems and eliminates a potential Redis bottleneck.
ID Generation Strategies:¶
AutoFieldMixin supports multiple ID generation strategies:
-
uuid4 (default): Random UUID4 hex string, 32 characters. Not time-sortable but provides excellent uniqueness without dependencies.
-
ulid: Universally Unique Lexicographically Sortable Identifier, 26 characters. Time-sortable, making it ideal for chronological ordering. Requires the ulid-py package: pip install ulid-py
-
ksuid: K-Sortable Unique Identifier, 27 characters. Similar to ULID but with a different encoding. Requires the cyksuid package: pip install cyksuid
Integration with Popoto's Field System:¶
AutoFieldMixin is designed for multiple inheritance with Field via the Method Resolution Order (MRO). It works with KeyFieldMixin and UniqueKeyField to create the full AutoKeyField:
class AutoKeyField(AutoFieldMixin, UniqueKeyField):
...
This layered approach allows each mixin to handle its specific concern: - AutoFieldMixin: ID generation and validation - KeyFieldMixin: Index maintenance via Redis Sets - UniqueKeyField: Uniqueness constraints
Automatic Model Enhancement:¶
When a model has no explicit KeyFields, Popoto automatically adds a hidden
_auto_key field of type AutoKeyField during model initialization. This
ensures every model can be uniquely identified and queried without forcing
developers to think about keys for simple use cases.
Example::
# Explicit AutoKeyField with default UUID4 strategy
class Article(popoto.Model):
uuid = popoto.AutoKeyField()
title = popoto.Field()
# Time-sortable ULID strategy
class Order(popoto.Model):
id = popoto.AutoKeyField(strategy="ulid")
product = popoto.Field()
# K-Sortable ID strategy
class Event(popoto.Model):
id = popoto.AutoKeyField(strategy="ksuid")
name = popoto.Field()
# Implicit _auto_key (added automatically since no KeyFields defined)
class LogEntry(popoto.Model):
message = popoto.Field()
# _auto_key is silently added
# All can be saved and retrieved:
article = Article.create(title="Hello")
print(article.uuid) # e.g., "a1b2c3d4e5f6..."
order = Order.create(product="Widget")
print(order.id) # e.g., "01ARZ3NDEKTSV4RRFFQ69G5FAV"
entry = LogEntry.create(message="Something happened")
print(entry._auto_key) # e.g., "f6e5d4c3b2a1..."
AutoFieldMixin
¶
Mixin that provides automatic ID generation for key fields.
This mixin adds auto-generation capabilities to any field, though it's typically used with KeyFieldMixin to create AutoKeyField. The auto-generated value becomes part of the Redis key, ensuring each model instance has a unique identifier.
Why a Mixin?¶
Using a mixin rather than direct inheritance allows composition of field behaviors. A field can be both auto-generated AND sorted, or auto-generated AND part of a compound key. This flexibility mirrors Django's field design while adapting to Redis's key-value paradigm.
ID Generation Strategies:¶
Supports multiple strategies via the strategy parameter:
-
"uuid4" (default): Random UUID4 hex string, 32 characters. No MAC address leakage (privacy), no clock synchronization requirements (distributed-friendly), and sufficient uniqueness (122 bits of randomness).
-
"ulid": Universally Unique Lexicographically Sortable Identifier, 26 chars. Time-sortable, ideal for chronological ordering. Requires ulid-py package.
-
"ksuid": K-Sortable Unique Identifier, 27 characters. Similar to ULID with different encoding. Requires cyksuid package.
The default 32-character length for UUID4 uses the full hex representation.
Shorter lengths can be configured via auto_uuid_length for cases where
collision probability is acceptable in exchange for shorter keys.
Trade-offs:¶
- AutoKeyFields skip Redis Set indexing (see on_save) because their values are unique and not useful for category-based queries
- Once generated, auto values should not be modified (not enforced, but changing them would create a new Redis key, orphaning the old data)
Attributes:
| Name | Type | Description |
|---|---|---|
auto |
bool
|
Boolean flag indicating this field auto-generates its value. Always True for this mixin; exists for field introspection. |
auto_uuid_length |
int
|
Length of the generated UUID string (default 32). Shorter values increase collision probability. Only applies to uuid4 strategy. |
auto_id |
str
|
Deprecated/unused attribute for potential future use. |
strategy |
str
|
ID generation strategy ("uuid4", "ulid", or "ksuid"). |
Source code in src/popoto/fields/auto_field_mixin.py
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 | |
is_valid(field, value, null_check=True, **kwargs)
classmethod
¶
Validate that the auto-generated value has the expected length.
This validation catches cases where a value was manually assigned with an incorrect length, which could indicate data corruption or a programming error. The length check happens before the parent class validation to fail fast on obviously invalid data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
field
|
The Field instance being validated. |
required | |
value
|
The value to validate (should be an ID string or None). |
required | |
null_check
|
If True, null values will be checked against field.null. |
True
|
|
**kwargs
|
Additional validation context (passed to parent). |
{}
|
Returns:
| Type | Description |
|---|---|
bool
|
True if valid, False otherwise. Logs an error on invalid length. |
Source code in src/popoto/fields/auto_field_mixin.py
get_new_auto_key_value()
¶
Generate a new identifier string based on the configured strategy.
Strategies: - uuid4 (default): Uses uuid.uuid4().hex for a lowercase hexadecimal string without hyphens, truncated to auto_uuid_length. URL-safe and Redis-key-safe characters. - ulid: Generates a ULID (Universally Unique Lexicographically Sortable Identifier) using the ulid-py package. Time-sortable, 26 characters. - ksuid: Generates a KSUID (K-Sortable Unique Identifier) using the cyksuid package. Time-sortable, 27 characters.
Returns:
| Type | Description |
|---|---|
|
A string identifier. Length depends on strategy: |
|
|
|
|
|
|
Raises:
| Type | Description |
|---|---|
ImportError
|
If ulid or ksuid strategy is used but the required package is not installed. |
Source code in src/popoto/fields/auto_field_mixin.py
set_auto_key_value(force=False)
¶
Set a new auto-generated value as this field's default.
Called during model instantiation to prepare a fresh UUID for new
instances. The value is stored as self.default so it gets applied
when the model sets field defaults.
This is called in Model.init for all fields with auto=True,
ensuring each new model instance starts with its own unique key.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
force
|
bool
|
If True, generate a new value even if auto=False. Useful for programmatically resetting a field's identity. |
False
|
Source code in src/popoto/fields/auto_field_mixin.py
on_save(model_instance, field_name, field_value, pipeline=None, **kwargs)
classmethod
¶
Handle post-save operations for auto-generated fields.
Intentionally a no-op that returns the pipeline unchanged. This overrides KeyFieldMixin.on_save to prevent auto-fields from being added to Redis Sets for value-based indexing.
Why skip indexing?¶
Auto-generated values are unique and random, making Set-based indexing pointless: you would have one Set per unique value, each containing exactly one model instance. The storage cost would be significant with no query benefit.
Instead, auto-key lookups go directly through the Redis key, which already contains the auto value (e.g., "Article:a1b2c3d4...").
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_instance
|
Model
|
The model being saved. |
required |
field_name
|
str
|
Name of this field on the model. |
required |
field_value
|
The auto-generated value being saved. |
required | |
pipeline
|
Pipeline
|
Redis pipeline for batched operations, or None. |
None
|
**kwargs
|
Additional context (ignored). |
{}
|
Returns:
| Type | Description |
|---|---|
|
The pipeline unchanged, or None if no pipeline was provided. |