-
-
Notifications
You must be signed in to change notification settings - Fork 485
What's the best way to write a type hint for any kind of model object? #1068
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
Comments
Try |
Thanks @sobolevn ! Do you mean something like this:
Let's say my class looks like this:
If my function is like this one, the issue is that the
Thanks again for your help and for any suggestions on the above use case! |
Can you please attach a reproducable example where it does not? |
@sobolevn thank you for your suggestion, and thank you for your patience! Here's the code you provided:
What I was hoping for was a way to type hint a "generic" django model that would automatically include whichever model that is being used's methods automatically, without me having to define the model methods in two places. At a minimum I would want it to include the fields that are present on For instance, I would hope this would work:
I've tried for instance this code but it doesn't seem to work:
Again, apologies if I'm being slow here and thank you very much for your suggestions! |
Have you tried TypeVar? TModel = TypeVar('TModel', bound=models.Model)
def dont_speak(my_model: TModel): ... |
@christianbundy thank you for your suggestion! How would I add the |
Does it work if you use |
Because of that, django-stubs only declare the pk attribute on @YPCrumble I think in your case you probably want to reference the pk because if one of your models were to set the primary_key on another field, you would have a runtime error: TModel = TypeVar('TModel', bound=models.Model)
class SomeModel(models.Model):
name = models.Charfield(primary_key=True)
def transform_model(some_object: TModel) -> TModel:
LOGGER.info("Transforming model with id %s", model.id)
return some_object
transform_model(SomeModel.objects.first()) # AttributeError: 'SomeModel' object has no attribute 'id' If nonetheless, you know for sure that every models will have an id, I'm not sure how you can declare that. I guess the pk tip should do the trick. |
Thanks for maintaining this repo! I have an issue that's likely a documentation issue in that I can't figure out how to create a type hint for a django model object that supports more than one model class.
I'm writing a type hint for a function that takes a model object of any type and performs an action on it, something like:
The issue I'm facing is that if I try to use
models.Model
as my type hint, I get errors like:as well as an error for any model methods I'm trying to use in the transformation function.
If I use a particular model class as the type hint it works fine, for instance
some_object: SomeParticularModel
. The issue is that I want to use this function for any type of model, not justSomeParticularModel
.Is there an appropriate way to be able to type hint a generic Django model?
The text was updated successfully, but these errors were encountered: