Skip to content

Conversation

@AghastyGD
Copy link
Owner

@AghastyGD AghastyGD commented Sep 21, 2025

Issue: #40

Problem
Django models using UUIDField, UUIDAutoField, or ForeignKey with a UUID PK were not properly serialized in lazy-ninja.
Pydantic schemas expected int or str, causing validation errors when UUIDs were returned directly.

Solution

  • Update get_pydantic_type to check UUIDField before AutoField, ensuring UUIDAutoField maps to str.
  • Update serialize_model_instance to convert UUIDField and ForeignKey (to UUID PKs) into strings.
  • Enhance convert_foreign_keys to handle both integer and string PKs.
  • Add parse_model_int, a new helper that converts string path parameters into the correct type for the model’s PK in CRUD routes.

Impact

  • Fixes Pydantic validation errors when returning UUID primary keys from models.
  • Supports projects that have DEFAULT_AUTO_FIELD = "core.fields.UUIDAutoField".
  • No breaking changes for models using standard AutoField PKs.

Example

class User(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    name = models.CharField(max_length=100)

class Post(models.Model):
    id = models.AutoField(primary_key=True)
    author = models.ForeignKey(User, on_delete=models.CASCADE)

# Post instance serialization
serialize_model_instance(post_instance)
# Returns: {'id': 1, 'author': 'd290f1ee-6c54-4b01-90e6-d701748f0851'}

Closes #40

@AghastyGD AghastyGD self-assigned this Sep 21, 2025
- Centralize path parameter parsing with `parse_model_id()`
- Ensure get, update, delete endpoints correctly handle int and UUID PKs
- Serialize UUIDField and ForeignKey to string for Pydantic compatibility
- Maintain backward compatibility for AutoField integer PKs
@AghastyGD AghastyGD merged commit 72c1d09 into dev Sep 21, 2025
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

UUIDField response not validated correctly (expects str but receives UUID

1 participant