Skip to content
This repository was archived by the owner on Jul 18, 2024. It is now read-only.

Allow abstract subclasses of ModelSchema#63

Open
mikulas-mrva wants to merge 4 commits into
jordaneremieff:mainfrom
mikulas-mrva:config-abstract-model
Open

Allow abstract subclasses of ModelSchema#63
mikulas-mrva wants to merge 4 commits into
jordaneremieff:mainfrom
mikulas-mrva:config-abstract-model

Conversation

@mikulas-mrva

Copy link
Copy Markdown

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
Copy Markdown
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
Copy Markdown
Author

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

@jordaneremieff

Copy link
Copy Markdown
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

mikulas-mrva commented Dec 31, 2022

Copy link
Copy Markdown
Author

@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.

@jordaneremieff jordaneremieff left a comment

Copy link
Copy Markdown
Owner

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.

Comment thread docs/usage.md

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

@jordaneremieff jordaneremieff Jan 7, 2023

Copy link
Copy Markdown
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

Comment thread docs/usage.md

### 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
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Rename ModelSchema.

Comment thread docs/usage.md
from djantic import ModelSchema
from myapp.models import User

class BaseModelSchema(ModelSchema):

Copy link
Copy Markdown
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.

Comment thread tests/test_main.py
model = User
include = ["id"]

schema = InheritedSchema(id=1)

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Add specific assertions.

Comment thread tests/test_main.py

class AbstractModelSchema(ModelSchema):
class Config:
abstract = True

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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 subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants