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 abstract subclasses of ModelSchema #63

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

mikulas-mrva
Copy link

Adding the option to implement custom methods in abstract ModelSchema subclasses that cannot be instantiated and thus they don't have to undergo the strict checks that regular ModelSchemata need to fulfill.

Closes !62

… subclasses that cannot be instantiated and thus they don't have to undergo the strict checks that regular ModelSchemata need to fulfill.

Closes !62
@jordaneremieff
Copy link
Owner

Thanks @mikulas-mrva. I'll need to fix some issue in GH workflow and try to review this on the weekend.

@mikulas-mrva
Copy link
Author

@jordaneremieff Any way I can help with the workflow issues?

@jordaneremieff
Copy link
Owner

@mikulas-mrva hey, sorry haven't had a chance to come back to this (that time of the year...). Sure if you want to open another PR that modifies the workflows file/passes then I can merge that and run it for this.

It looks like one of more of the Python versions specified for the tests is no longer available.

@mikulas-mrva
Copy link
Author

mikulas-mrva commented Dec 31, 2022

@jordaneremieff I have changed 3.10.0 to 3.10 and added 3.11 now that it's out, but seems I need your approval to see if it helped.

Copy link
Owner

@jordaneremieff jordaneremieff left a comment

Choose a reason for hiding this comment

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

Thanks @mikulas-mrva. I've added my review comments with some requested changes.

@@ -40,6 +40,33 @@ class UserSchema(ModelSchema):

Once defined, the `UserSchema` can be used to perform various functions on the underlying Django model object, such as generating JSON schemas or exporting serialized instance data.

### Custom subclasses
Copy link
Owner

Choose a reason for hiding this comment

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

Changes:

  1. Make this heading ### Abstract schema models
  2. Place as the next heading after Customizing the schema

@@ -40,6 +40,33 @@ class UserSchema(ModelSchema):

Once defined, the `UserSchema` can be used to perform various functions on the underlying Django model object, such as generating JSON schemas or exporting serialized instance data.

### Custom subclasses

Abstract subclasses can be defined to implement methods shared over for a number of ModelSchemata, note that they cannot be instantiated by themselves.
Copy link
Owner

Choose a reason for hiding this comment

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

Rename ModelSchema.

from djantic import ModelSchema
from myapp.models import User

class BaseModelSchema(ModelSchema):
Copy link
Owner

Choose a reason for hiding this comment

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

Can you make more obvious that this is an example and what the use-case is? This may be misinterpreted of how to define all abstract schema. For example, the model: Optional[Model] = None isn't required, but not clear.

model = User
include = ["id"]

schema = InheritedSchema(id=1)
Copy link
Owner

Choose a reason for hiding this comment

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

Add specific assertions.

@@ -37,6 +37,23 @@ class Config:
include = ["id"]
exclude = ["first_name"]

class AbstractModelSchema(ModelSchema):
class Config:
abstract = True
Copy link
Owner

Choose a reason for hiding this comment

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

What is expected to happen when model, include, or exclude are set on the config? Need coverage for these cases.

Copy link
Author

Choose a reason for hiding this comment

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

This is a very good point, I'll ping you once I have a minute to look into it.

In my opinion none of those should be present on an abstract model, as its main raison d'etre is to reduce boilerplate and allow proper typing in codebases where multiple ModelSchemata are used in a similar customised manner. Do you have any thoughts on the matter as the repo/package owner?

Copy link
Owner

Choose a reason for hiding this comment

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

I guess it would be best if we started out with some warnings (raise config error) if someone tries to include these on the abstract model to avoid confusion, then if someone later presented a valid use-case to support any of these then maybe could do then if it made sense.

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.

None yet

2 participants