django getattr():属性名必须是字符串?

9ceoxa92  于 2023-06-07  发布在  Go
关注(0)|答案(1)|浏览(166)

我的models.py文件:

class Simple(models.Model):
    class Inner(models.TextChoices):
        a = 'small','Small'
        b = 'medium','Medium'
        c = 'large','Large'
    id = models.UUIDField(primary_key=True,default=uuid.uuid4,
        editable=False)
    name = models.CharField(max_length=200)
    size = models.TextField(choices=Inner.choices,default='medium',
        db_index=True)

当使用管理界面添加我得到:
类型错误:/admin/main/simple/add/
getattr():属性名称必须为字符串
这是我在cmd中的错误:

Traceback (most recent call last):
      File "C:\Users\1\Desktop\real\django3.1\venv\lib\site-packages\django\core\han
    dlers\exception.py", line 47, in inner
        response = get_response(request)
      File "C:\Users\1\Desktop\real\django3.1\venv\lib\site-packages\django\core\han
    dlers\base.py", line 179, in _get_response
        response = wrapped_callback(request, *callback_args, **callback_kwargs)
   extra_context)
      File "C:\Users\1\Desktop\real\django3.1\venv\lib\site-packages\django\utils\de
    corators.py", line 43, in _wrapper
        return bound_method(*args, **kwargs)
      File "C:\Users\1\Desktop\real\django3.1\venv\lib\site-packages\django\utils\de
    corators.py", line 130, in _wrapped_view
        response = view_func(request, *args, **kwargs)
      File "C:\Users\1\Desktop\real\django3.1\venv\lib\site-packages\django\contrib\
    admin\options.py", line 1534, in changeform_view
        return self._changeform_view(request, object_id, form_url, extra_context)
      File "C:\Users\1\Desktop\real\django3.1\venv\lib\site-packages\django\contrib\
    admin\options.py", line 1573, in _changeform_view
        form_validated = form.is_valid()
      File "C:\Users\1\Desktop\real\django3.1\venv\lib\site-packages\django\forms\fo
    rms.py", line 177, in is_valid
        return self.is_bound and not self.errors
      File "C:\Users\1\Desktop\real\django3.1\venv\lib\site-packages\django\forms\fo
    rms.py", line 172, in errors
        self.full_clean()
      File "C:\Users\1\Desktop\real\django3.1\venv\lib\site-packages\django\forms\fo
    rms.py", line 376, in full_clean
        self._post_clean()
      File "C:\Users\1\Desktop\real\django3.1\venv\lib\site-packages\django\forms\mo
    dels.py", line 411, in _post_clean
        self.validate_unique()
      File "C:\Users\1\Desktop\real\django3.1\venv\lib\site-packages\django\forms\mo
    dels.py", line 420, in validate_unique
        self.instance.validate_unique(exclude=exclude)
      File "C:\Users\1\Desktop\real\django3.1\venv\lib\site-packages\django\db\model
    s\base.py", line 1007, in validate_unique
        date_errors = self._perform_date_checks(date_checks)
      File "C:\Users\1\Desktop\real\django3.1\venv\lib\site-packages\django\db\model
    s\base.py", line 1124, in _perform_date_checks
        date = getattr(self, unique_for)
    TypeError: getattr(): attribute name must be string

当我使用py manage.py shell创建时没有问题,但是当我使用管理界面时,我得到了错误。

jtjikinw

jtjikinw1#

您没有正确设置选项的默认值default= Inner.b

变更:

size = models.TextField(choices=Inner.choices,default='medium',
        db_index=True)

收件人:

size = models.TextField(choices=Inner.choices,default= Inner.b,
        db_index=True)

相关问题