django-ninja
django-ninja copied to clipboard
[BUG] Running tests using Schema model generates warning
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
I agree. This seems like a simple solution to get rid of a pesky warning. What do you think about it @vitalik ?
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
Fixed with https://github.com/vitalik/django-ninja/pull/1528 ?
Let me test it out.
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.
@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"]
class Config is fully removed in 1.5+ - please use Meta instead