Django的Field的"default"和test的"client"参数从哪里来?

Where do Django's Field's "default" and test's "client" parameters come from?

如果通过

在Django中搜索Field(如IntergField)的参数
from django.db import models 
dir(models.IntegerField)

你得到 'default_error_messages'、'default_validators'、'unique'、'validate'、'validators' 等,但不是“默认”本身,尽管它很常用, 如

class Choice(models.Model):
    ...
    votes = models.IntegerField(default=0)

“客户”也一样。文档说 TestCase “带有它自己的客户端”。在 Django 文档的这个片段中,探索了这个客户端

class QuestionIndexViewTests(TestCase):
    def test_no_questions(self):
        """
        If no questions exist, an appropriate message is displayed.
        """
        response = self.client.get(reverse('polls:index'))

但是通过

找不到
from django.test import TestCase
dir(django.test.TestCase)

甚至

dir(django.test.TestCase.client_class)

我问的是它们从哪里来,还有如何搜索这些“隐藏”的参数、方法等

.default 不是 class 的属性,而是 实例 的属性。事实上,如果我们查看 source code [GitHub],我们会看到:

class Field(RegisterLookupMixin):
    # …

    def __init__(self, verbose_name=None, name=None, primary_key=False,
                 max_length=None, unique=False, blank=False, null=False,
                 db_index=False, rel=None, default=NOT_PROVIDED, editable=True,
                 serialize=True, unique_for_date=None, unique_for_month=None,
                 unique_for_year=None, choices=None, help_text='', db_column=None,
                 db_tablespace=None, auto_created=False, validators=(),
                 error_messages=None):
        # …
        <strong>self.default = default</strong>
        # …

因此,如果你构造一个 IntegerField 对象,你可以找到这个:

>>> from django.db.models import IntegerField
>>> dir(IntegerField())
['__class__', '__copy__', '__deepcopy__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_check_backend_specific_checks', '_check_choices', '_check_db_index', '_check_deprecation_details', '_check_field_name', '_check_max_length_warning', '_check_null_allowed_for_primary_keys', '_check_validators', '_choices_is_value', '_clear_cached_lookups', '_db_tablespace', '_description', '_error_messages', '_get_default', '_get_flatchoices', '_get_lookup', '_unique', '_unregister_lookup', '_validators', '_verbose_name', 'auto_created', 'auto_creation_counter', 'blank', 'cached_col', 'cast_db_type', 'check', 'choices', 'class_lookups', 'clean', 'clone', 'contribute_to_class', 'creation_counter', 'db_check', 'db_column', 'db_index', 'db_parameters', 'db_returning', 'db_tablespace', 'db_type', 'db_type_parameters', 'db_type_suffix', 'deconstruct', <strong>'default'</strong>, 'default_error_messages', 'default_validators', 'description', 'descriptor_class', 'editable', 'empty_strings_allowed', 'empty_values', 'error_messages', 'flatchoices', 'formfield', 'get_attname', 'get_attname_column', 'get_choices', 'get_col', 'get_db_converters', 'get_db_prep_save', 'get_db_prep_value', 'get_default', 'get_filter_kwargs_for_object', 'get_internal_type', 'get_lookup', 'get_lookups', 'get_pk_value_on_save', 'get_prep_value', 'get_transform', 'has_default', 'help_text', 'hidden', 'is_relation', 'many_to_many', 'many_to_one', 'max_length', 'merge_dicts', 'name', 'null', 'one_to_many', 'one_to_one', 'pre_save', 'primary_key', 'register_lookup', 'rel_db_type', 'related_model', 'remote_field', 'run_validators', 'save_form_data', 'select_format', 'serialize', 'set_attributes_from_name', 'system_check_deprecated_details', 'system_check_removed_details', 'to_python', 'unique', 'unique_for_date', 'unique_for_month', 'unique_for_year', 'validate', 'validators', 'value_from_object', 'value_to_string', 'verbose_name']

此处 .default 属性可见。

对于 TestCase,这是由 SimpleTestCase 中实现的逻辑处理的。这将在运行测试用例时设置测试用例,这包括使用 .client 填充对象,该 .clientclient_class class 引用的 class 的实例属性

class SimpleTestCase(unittest.TestCase):
    # …
    <strong>client_class = Client</strong>

    def __call__(self, result=None):
        """
        Wrapper around default __call__ method to perform common Django test
        set up. This means that user-defined Test Cases aren't required to
        include a call to super().setUp().
        """
        <strong>self._setup_and_call(result)</strong>

    def _setup_and_call(self, result, debug=False):
        # …
        <strong>self._pre_setup()</strong>
        # …
    
    def _pre_setup(self):
        # …
        <strong>self.client = self.client_class()</strong>
        # …

class TransactionTestCase(SimpleTestCase):
    # …

TestCase(TransactionTestCase):
    # …

这并不少见。在 Python 中,属性通常是在对象上定义的,或者甚至在 运行 测试用例时稍后通过某种方法定义。这个只有看文档才能知道