Implement support for <QuerySet>.as_manager() (#1025)

* Implement support for `<QuerySet>.as_manager()`

* fixup! Implement support for `<QuerySet>.as_manager()`

* fixup! fixup! Implement support for `<QuerySet>.as_manager()`
This commit is contained in:
Petter Friberg
2022-09-29 14:05:25 +02:00
committed by GitHub
parent 1f2e406972
commit 54d5835f66
8 changed files with 582 additions and 82 deletions
+8 -10
View File
@@ -145,9 +145,7 @@ And then use `AuthenticatedHttpRequest` instead of the standard `HttpRequest` fo
### My QuerySet methods are returning Any rather than my Model
`QuerySet.as_manager()` is not currently supported.
If you are using `MyQuerySet.as_manager()`, then your `Manager`/`QuerySet` methods will all not be linked to your model.
If you are using `MyQuerySet.as_manager()`:
Example:
@@ -163,12 +161,12 @@ class MyModel(models.Model):
objects = MyModelQuerySet.as_manager()
def use_my_model():
foo = MyModel.objects.get(id=1) # This is `Any` but it should be `MyModel`
return foo.xyz # No error, but there should be
def use_my_model() -> int:
foo = MyModel.objects.get(id=1) # Should now be `MyModel`
return foo.xyz # Gives an error
```
There is a workaround: use `Manager.from_queryset` instead.
Or if you're using `Manager.from_queryset`:
Example:
@@ -188,9 +186,9 @@ class MyModel(models.Model):
objects = MyModelManager()
def use_my_model():
foo = MyModel.objects.get(id=1)
return foo.xyz # Gives an error
def use_my_model() -> int:
foo = MyModel.objects.get(id=1) # Should now be `MyModel`
return foo.xyz # Gives an error
```
### How do I annotate cases where I called QuerySet.annotate?