Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #16, we don't need to set model_fields #17

Merged
merged 1 commit into from
Dec 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions djantic/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
from enum import Enum
from functools import reduce
from itertools import chain
from typing import Any, Dict, List, Optional, no_type_check, Union
from typing_extensions import get_origin, get_args
from typing import Any, Dict, List, Optional, Union, no_type_check

from django.core.serializers.json import DjangoJSONEncoder
from django.db.models import Manager, Model
Expand All @@ -13,8 +12,9 @@
from django.utils.encoding import force_str
from django.utils.functional import Promise
from pydantic import BaseModel, create_model
from pydantic.errors import PydanticUserError
from pydantic._internal._model_construction import ModelMetaclass
from pydantic.errors import PydanticUserError
from typing_extensions import get_args, get_origin

if sys.version_info >= (3, 10):
from types import UnionType
Expand Down Expand Up @@ -124,7 +124,6 @@ def __new__(mcs, name: str, bases: tuple, namespace: dict, **kwargs):
field_values[field_name] = (python_type, pydantic_field)

cls.__doc__ = namespace.get("__doc__", config["model"].__doc__)
cls.model_fields = {}
cls.__alias_map__ = {
getattr(model_field[1], "alias", None) or field_name: field_name
for field_name, model_field in field_values.items()
Expand Down
25 changes: 14 additions & 11 deletions tests/test_fields.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
from typing import Optional

import pytest
from pydantic import ConfigDict
from testapp.models import Configuration, Listing, Preference, Record, Searchable, User, NullableChar, NullableFK

from pydantic import (
ValidationInfo,
field_validator,
ValidationError,
from pydantic import ConfigDict, ValidationError, ValidationInfo, field_validator
from testapp.models import (
Configuration,
Listing,
NullableChar,
NullableFK,
Preference,
Record,
Searchable,
User,
)

from djantic import ModelSchema
Expand Down Expand Up @@ -240,13 +243,13 @@ class RecordSchema(ModelSchema):
"description": "A generic record model.",
"properties": {
"record_type": {
"allOf": [{"$ref": "#/$defs/RecordSchemaRecordTypeEnum"}],
"$ref": "#/$defs/RecordSchemaRecordTypeEnum",
"default": "NEW",
"description": "record_type",
"title": "Record Type",
},
"record_status": {
"allOf": [{"$ref": "#/$defs/RecordSchemaRecordStatusEnum"}],
"$ref": "#/$defs/RecordSchemaRecordStatusEnum",
"default": 0,
"description": "record_status",
"title": "Record Status",
Expand Down Expand Up @@ -300,13 +303,13 @@ class PreferenceSchema(ModelSchema):
"type": "string",
},
"preferred_food": {
"allOf": [{"$ref": "#/$defs/PreferenceSchemaPreferredFoodEnum"}],
"$ref": "#/$defs/PreferenceSchemaPreferredFoodEnum",
"default": "ba",
"description": "preferred_food",
"title": "Preferred Food",
},
"preferred_group": {
"allOf": [{"$ref": "#/$defs/PreferenceSchemaPreferredGroupEnum"}],
"$ref": "#/$defs/PreferenceSchemaPreferredGroupEnum",
"default": 1,
"description": "preferred_group",
"title": "Preferred Group",
Expand Down
Loading