-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove redundant instance variables "context" and "instance" (#11)
The following two instance variables "context" and "instance" are no longer set in the "from_django" function as these have no use. The "dump" parameter is also removed, as it was added by mistake in the refactor to accomodate pydantic 2. fixes #10
- Loading branch information
1 parent
65e717b
commit 7cc6214
Showing
2 changed files
with
37 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,12 @@ | |
from pydantic import ConfigDict | ||
from testapp.models import Configuration, Listing, Preference, Record, Searchable, User | ||
|
||
from pydantic import ( | ||
ValidationInfo, | ||
field_validator, | ||
ValidationError, | ||
) | ||
|
||
from djantic import ModelSchema | ||
|
||
|
||
|
@@ -33,6 +39,34 @@ class UserSchema(ModelSchema): | |
} | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_context_for_field(): | ||
|
||
def get_context(): | ||
return {'check_title': lambda x: x.istitle()} | ||
|
||
class UserSchema(ModelSchema): | ||
model_config = ConfigDict( | ||
model=User, | ||
revalidate_instances='always' | ||
) | ||
|
||
@field_validator('first_name', mode="before", check_fields=False) | ||
@classmethod | ||
def validate_first_name(cls, v: str, info: ValidationInfo): | ||
if not info.context: | ||
return v | ||
|
||
check_title = info.context.get('check_title') | ||
if check_title and not check_title(v): | ||
raise ValueError('First name needs to be a title') | ||
return v | ||
|
||
user = User.objects.create(first_name="hello", email="[email protected]") | ||
with pytest.raises(ValidationError): | ||
UserSchema.from_django(user, context=get_context()) | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_unhandled_field_type(): | ||
class SearchableSchema(ModelSchema): | ||
|