Skip to content

Add attributes to the Model and Form classes (#83) #85

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

Closed
wants to merge 1 commit into from
Closed

Add attributes to the Model and Form classes (#83) #85

wants to merge 1 commit into from

Conversation

kam1sh
Copy link

@kam1sh kam1sh commented Jun 2, 2019

This commit fixes type error in Model 'objects' (#83) and also adds 'Meta' inner class to the ModelForm, that is used to reference model and its fields (docs)

* ModelForm could have 'Meta' inner class that references a model
  https://docs.djangoproject.com/en/2.2/topics/forms/modelforms/#modelform

* And Model always have 'objects' field
@@ -13,6 +13,7 @@ class Model(metaclass=ModelBase):
_meta: Any
_default_manager: Manager[Model]
pk: Any = ...
objects: Manager[Model]
Copy link
Member

Choose a reason for hiding this comment

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

Please, take a look at the https://github.com/mkurnikov/django-stubs/pull/20 and _default_manager in the Django docs. _default_manager is the reason why there's no objects attribute on the Model class.

Copy link
Author

@kam1sh kam1sh Jun 2, 2019

Choose a reason for hiding this comment

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

Um... then I don't understand. I should somehow define objects as alias to the _default_manager? Because if there is no objects then mypy will always fail in cases like mine...

Copy link
Author

Choose a reason for hiding this comment

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

Maybe I can define objects and _default_manager as a TypeVar("_T", bound=Manager)? Will it be appropriate?

Copy link
Contributor

Choose a reason for hiding this comment

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

You probably already know this, but just in case not, you can hint objects as objects: models.Manager and it will work generically for everything, though it won't be as specific as something like Manager[Model]. We use this in our codebase a lot.

@mkurnikov
Copy link
Member

I addressed Form fix in #107, objects is not really that easy, closing it for now.

@mkurnikov mkurnikov closed this Jul 10, 2019
voidus pushed a commit to voidus/django-stubs that referenced this pull request Apr 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants