erhosen-libs / pydjantic Goto Github PK
View Code? Open in Web Editor NEWUse Pydantic Settings in your Django application.
License: MIT License
Use Pydantic Settings in your Django application.
License: MIT License
When settings contains types that are not covered with to_django
it is passes as-is to underlying variables. So if settings contains any of these types (and much more, any custom):
it will lead to unexpected behaviour since Django code and apps knows nothing about these types. This can be easily solved by calling settings.model_dump(mode="json")
instead of settings.model_dump()
(which targets mode="python"
by default), however I have no idea how to force lazy gettext strings to pass this call. Or if that's even possible.
pydantic_core._pydantic_core.PydanticSerializationError: Unable to serialize unknown type: <class 'django.utils.functional.lazy.<locals>.__proxy__'>
Hi, would you mind changing minimal Requires-Python >=3.8?
I receive the following failure output:
SECRET_KEY
field required (type=value_error.missing)
The SECRET_KET
config looks as follows:
SECRET_KEY: str = Field(default=Undefined, env='DJANGO_SECRET_KEY')
Given I'm using environment variables to pass in the configuration I can not discern the issue from looking at the output, e.g. I don't know I have to provide a value in DJANGO_SECRET_KEY
until looking into the code.
Would it be possible to provide more details, possibly the entire config for this field? I see there are title
and description
available, which could be helpful too:
SECRET_KEY: str = Field(default=Undefined, env='DJANGO_SECRET_KEY', title="Secret key", description="lorem ipsum...")
Possible solution:
SECRET_KEY
Error: field required (type=value_error.missing)
Title: Secret Key
Description: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed suscipit, orci
nec facilisis tristique, nibh leo pretium mi, sed consequat nibh magna sit amet elit. Maecenas
tempus porta tellus eu feugiat. Phasellus eget vehicula libero. Donec eget augue
pellentesque, condimentum elit sed, lobortis est. Fusce eu risus placerat, mollis dolor at,
convallis mi.
Env: DJANGO_SECRET_KEY
Hello, from what I can see, types are implemented, so, would it be possible to add the py.typed file for pylance, mypy and other type checker to work with this lib ?
We should be able to set conn_max_age & ssl_require in BaseDBConfig.
Hi!
I like this project! However I'm curious what's your approach on environments differences. For instance we might want to add some app or middleware only to development env or specifically define some settings in production while keeping django defaults for test.
Would be great to be able to bootstrap a .env
based of the defined structure. Ideally interactive and non-interactive.
Even when running tests:
tests/test_to_django.py::test_to_django_settings
D:\NVIDIA\third-party\_python\pydjantic\.venv\Lib\site-packages\pydantic\main.py:308: UserWarning: Pydantic serializer warnings:
Expected `multi-host-url` but got `dict` - serialized value may not be as expected
return self.__pydantic_serializer__.to_python(
tests/test_db_config.py::test_dsn_and_exact_config
D:\NVIDIA\third-party\_python\pydjantic\.venv\Lib\site-packages\pydantic\main.py:308: UserWarning: Pydantic serializer warnings:
Expected `str` but got `dict` - serialized value may not be as expected
return self.__pydantic_serializer__.to_python(
Seems to come from database configuration, e.g. (in test_dsn_and_exact_config
):
class DatabaseConfig(BaseDBConfig):
# declared as str, but actually deserialized as dict (Django database configuration)❓
default: str = Field(default="postgres://user:password@hostname:5432/dbname")
replica: PostgresDB = PostgresDB()
Applications still run, however:
-Werror
python interpreter flagConsider the following settings.py
:
import os
from pydantic import BaseSettings, SecretStr
from pydjantic import to_django
class Settings(BaseSettings):
MY_SECRET: SecretStr
# Define a secret in the env so that Pydantic validation passes
os.environ["MY_SECRET"] = "i am a secret"
settings = Settings()
print(settings.dict()) # ✅ Will NOT print the actual secret value, which is good
to_django(settings)
print(MY_SECRET) # ❌ Prints "**********", which is incorrect
Here is the output of running the above snippet:
{'MY_SECRET': SecretStr('**********')}
**********
The second print
should have yielded i am a secret
instead. Otherwise there is no way of using the secret value in the rendered settings.
pydjantic/tests/test_db_config.py
Line 79 in e7c4565
Right now we're testing Pydantic's serialization only with no Pydjantic code.
It should be DatabaseConfig(BaseDBConfig)
.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.