django-ninja icon indicating copy to clipboard operation
django-ninja copied to clipboard

[BUG] Running tests using Schema model generates warning

Open Andrioden opened this issue 1 year ago • 2 comments

Describe the bug If i run tests using pytest, when I have a test that uses a model inheriting from the Schema class pytest, a deprecation warning is generated.

This is due to this code that is not pydantic 2 standard (src)

class Schema(BaseModel, metaclass=ResolverMetaclass):
    class Config:
        from_attributes = True  # aka orm_mode

Gives ->

 PydanticDeprecatedSince20: Support for class-based `config` is deprecated, use ConfigDict instead. Deprecated in Pydantic V2.0 to be removed in V3.0. See Pydantic V2 Migration Guide at https://errors.pydantic.dev/2.8/migration/
    warnings.warn(DEPRECATION_MESSAGE, DeprecationWarning)

Fix:

class Schema(BaseModel, metaclass=ResolverMetaclass):
    model_config = ConfigDict(from_attributes =True)  # aka orm_mode

I was about to create a pull request, but i see the class is inherited from in examples ie class Config(Schema.Config), making it a backwards incompatible change, so I will leave it to you to consider how to move forward.

Versions (please complete the following information):

  • Python version: 3.12.1
  • Django version: 5.0.7
  • Django-Ninja version: 1.2.2
  • Pydantic version: 2.8.2

Andrioden avatar Jul 29 '24 22:07 Andrioden

I agree. This seems like a simple solution to get rid of a pesky warning. What do you think about it @vitalik ?

DamianB-BitFlipper avatar Nov 27 '24 17:11 DamianB-BitFlipper

well warnings are a bit intentional - we wanted to make migration from ninja 0.x to 1.x easier - so both Config and Meta are supported, but eventually (I guess 1.4 or 1.5) Config will be gone in favour of Meta

vitalik avatar Nov 28 '24 10:11 vitalik

Fixed with https://github.com/vitalik/django-ninja/pull/1528 ?

picturedots avatar Sep 22 '25 19:09 picturedots

Let me test it out.

neverspillover avatar Sep 23 '25 03:09 neverspillover

Let me test it out.

I can still reproduce this issue in the latest version (1.4.3). It seems that the latest changes may not have been released to the official version yet.

neverspillover avatar Sep 23 '25 03:09 neverspillover

@picturedots @neverspillover

Hello! I found the fix for the deprecation error. Might not be a catch all aproach. Use model_config as proposed in pydantic v2 but use the class Meta with fields and fields_optional instead of model_fields and model_fields_optional. If you need more info about the changes check the MD file:

https://github.com/vitalik/django-ninja/blob/bdb778f6f397873b06d932134476290aeaddc948/docs/docs/guides/response/config-pydantic.md

Example:

from pydantic import ConfigDict

class UserSchema(ModelSchema):
    model_config = ConfigDict(
        alias_generator = to_camel
        populate_by_name = True,  # !!!!!! <--------
    )
    class Meta:
        model = User
        fields = ["id", "email", "is_staff"]

dcrazyx59 avatar Oct 03 '25 09:10 dcrazyx59

class Config is fully removed in 1.5+ - please use Meta instead

vitalik avatar Oct 14 '25 15:10 vitalik