djantic2 icon indicating copy to clipboard operation
djantic2 copied to clipboard

field_validators don't working in djantic ?

Open Izcarmt95 opened this issue 7 months ago • 0 comments

I test this peace of code and I noticed that the field_validator does not work properly, never execute the field_validator function.

from djantic import ModelSchema
from pydantic import field_validator
from django.db import models

class Organization(models.Model):
    name = models.CharField(max_length=255)
    email = models.EmailField()
    country_code = models.CharField(max_length=10)
    phone = models.CharField(max_length=20)
    active = models.BooleanField(default=True)
    created_at = models.DateTimeField(auto_now_add=True)
    modified_at = models.DateTimeField(auto_now=True)

    def __str__(self):
        return self.name

class OrganizationSchema(ModelSchema):
    test_u: Optional[str] = None

    class Config:
        model = Organization
        from_attributes = True  # Para Pydantic v2

        include = (
            'id',
            'name',
            'email',
            'country_code',
            'phone',
            'active',
            'created_at',
            'modified_at',
        )

    @field_validator('test_u)
    @classmethod
    def validate_test_u(cls, value: Optional[str]) -> Optional[str]:
        raise ValueError('test_u is not allowed')

I try to include the field test_u in the include list , but is the same behavior.

Izcarmt95 avatar May 28 '25 04:05 Izcarmt95