I've found the reason why I've been thinking this wrong way. 

`getattr(self.get_queryset(), name)` is getting a bound method from 
Manager.get_queryset() which is a QuerySet class instance, while method is 
getting function from _queryset_class.

The difference is one of them is bound method, while the other is function.

在2024年2月28日星期三 UTC+8 10:13:09<Tim Graham> 写道:

> Hi Ryoma, a good way to investigate this would be to make the change and 
> see if anything breaks. 
>
> If I understand correctly, your suggest is:
>
> -                return getattr(self.get_queryset(), name)(*args, **kwargs)
> +                return method(*args, **kwargs)
>
> In that case, running the test suite gives this error:
>
>   File "/home/tim/code/django/django/db/models/manager.py", line 89, in 
> manager_method
>     return method(*args, **kwargs)
>            ^^^^^^^^^^^^^^^^^^^^^^^
> TypeError: QuerySet.using() missing 1 required positional argument: 'alias'
>
> I leave further investigation to you. :-)
> On Tuesday, February 27, 2024 at 7:28:05 PM UTC-5 Ryoma Han wrote:
>
>> I've been reading through the Django source code the last few days. When 
>> I was reading the BaseManager source code I saw a method like this:
>>
>> ```python
>>     @classmethod
>>     def _get_queryset_methods(cls, queryset_class):
>>         def create_method(name, method):
>>             @wraps(method)
>>             def manager_method(self, *args, **kwargs):
>>                 return getattr(self.get_queryset(), name)(*args, **kwargs)
>>
>>             return manager_method
>>
>>         new_methods = {}
>>         for name, method in inspect.getmembers(
>>             queryset_class, predicate=inspect.isfunction
>>         ):
>>             if hasattr(cls, name):
>>                 continue
>>             queryset_only = getattr(method, "queryset_only", None)
>>             if queryset_only or (queryset_only is None and 
>> name.startswith("_")):
>>                 continue
>>             new_methods[name] = create_method(name, method)
>>         return new_methods
>> ```
>>
>> My question is why we use `getattr(self.get_queryset(), name)` insted of 
>> just use `method`.
>>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers  (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-developers+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-developers/b6b6ea24-3baa-461c-b5b2-27f1128eb92bn%40googlegroups.com.

Reply via email to