mirror of
https://github.com/davidhalter/django-stubs.git
synced 2025-12-23 12:21:28 +08:00
Add support for inline from_queryset in model classes (#1045)
* Add support for inline from_queryset in model classes
This adds support for calling <Manager>.from_queryset(<QuerySet>)()
inline in models, for example like this:
class MyModel(models.Model):
objects = MyManager.from_queryset(MyQuerySet)()
This is done by inspecting the class body in the transform_class_hook
* Fix missing methods on copied manager
* Add test and other minor tweaks
* Always create manager at module level
When the manager is added at the class level, which happened when it was
created inline in the model body, it's not possible to retrieve the
manager again based on fullname. That lead to problems with inheritance
and the default manager.
This commit is contained in:
@@ -678,7 +678,6 @@
|
||||
def custom(self) -> None:
|
||||
pass
|
||||
|
||||
# Note, that we cannot resolve dynamic calls for custom managers:
|
||||
class Transaction(models.Model):
|
||||
objects = BaseManager.from_queryset(TransactionQuerySet)
|
||||
def test(self) -> None:
|
||||
@@ -689,9 +688,8 @@
|
||||
class TransactionLog(models.Model):
|
||||
transaction = models.ForeignKey(Transaction, on_delete=models.CASCADE)
|
||||
out: |
|
||||
myapp/models:9: error: `.from_queryset` called from inside model class body
|
||||
myapp/models:11: note: Revealed type is "django.db.models.manager.RelatedManager[myapp.models.TransactionLog]"
|
||||
myapp/models:13: note: Revealed type is "Any"
|
||||
myapp/models:10: note: Revealed type is "django.db.models.manager.RelatedManager[myapp.models.TransactionLog]"
|
||||
myapp/models:12: note: Revealed type is "Any"
|
||||
|
||||
|
||||
- case: resolve_primary_keys_for_foreign_keys_with_abstract_self_model
|
||||
|
||||
Reference in New Issue
Block a user