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

Allow related fields to be nullable #7

Merged

Conversation

marcy-buccellato
Copy link
Contributor

  • Allow related fields to be nullable by checking blank or null kwargs and adding Union[type, None]
  • Fix Enum representation by returning the myvar.value instead of the enum itself

@@ -102,7 +102,16 @@ def ModelSchemaField(field: Any, schema_name: str) -> tuple:
description = None
title = None
max_length = None
python_type = None
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unrelated, was unused

@@ -102,7 +102,16 @@ def ModelSchemaField(field: Any, schema_name: str) -> tuple:
description = None
title = None
max_length = None
python_type = None

try:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved this up here so it can be used for any field (related, or not)

@@ -172,6 +173,8 @@ def get(self, key: Any, default: Any = None) -> Any:
attr = list(attr.all())
elif outer_type_ == int and issubclass(type(attr), Model):
attr = attr.id
elif inspect.isclass(type(attr)) and issubclass(type(attr), Enum):
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated to the nullable related field change: the tests/test_fields.py::test_enum_choices test was failing (even on main branch. This should fix that.

Test fail:

FAILED tests/test_fields.py::test_enum_choices - pydantic_core._pydantic_core.ValidationError: 2 validation errors for PreferenceSchema
preferred_food
  Input should be 'ba' or 'ap' [type=enum, input_value=<FoodChoices.BANANA: 'ba'>, input_type=FoodChoices]
    For further information visit https://errors.pydantic.dev/2.7/v/enum
preferred_group
  Input should be 1 or 2 [type=enum, input_value=<GroupChoices.GROUP_1: 1>, input_type=GroupChoices]
    For further information visit https://errors.pydantic.dev/2.7/v/enum

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice.

@@ -281,3 +281,4 @@ class Case(ExtendedModel):

class Listing(models.Model):
items = ArrayField(models.TextField(), size=4)
content_type = models.ForeignKey(ContentType, on_delete=models.PROTECT, blank=True, null=True)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Allows us to test that blank/null flags are honored.

@marcy-buccellato
Copy link
Contributor Author

@jonathan-s this is ready for review please

@marcy-buccellato marcy-buccellato force-pushed the make_related_fields_nullable branch from 7c060e0 to 4f0b574 Compare May 8, 2024 13:07
@jonathan-s jonathan-s merged commit 263008b into jonathan-s:main May 9, 2024
5 checks passed
@jonathan-s
Copy link
Owner

Thank you for the PR, and I also appreciate that you took the time to comment some parts in the PR.

@marcy-buccellato
Copy link
Contributor Author

Thanks for the fast merge @jonathan-s , will you cut a pypi release soon?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants