英文:
getattr(): attribute name must be string?
问题
我的models.py文件:
class Simple(models.Model):
class Inner(models.TextChoices):
a = 'small', '小型'
b = 'medium', '中型'
c = '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)
在使用管理员界面添加时出现以下错误:
TypeError at
/admin/main/simple/add/
getattr(): attribute name must be string
这是我的命令行中的错误:
Traceback (most recent call last):
File "C:\Users\Desktop\real\django3.1\venv\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
response = get_response(request)
File "C:\Users\Desktop\real\django3.1\venv\lib\site-packages\django\core\handlers\base.py", line 179, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
extra_context)
File "C:\Users\Desktop\real\django3.1\venv\lib\site-packages\django\utils\decorators.py", line 43, in _wrapper
return bound_method(*args, **kwargs)
File "C:\Users\Desktop\real\django3.1\venv\lib\site-packages\django\utils\decorators.py", line 130, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "C:\Users\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\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\Desktop\real\django3.1\venv\lib\site-packages\django\forms\forms.py", line 177, in is_valid
return self.is_bound and not self.errors
File "C:\Users\Desktop\real\django3.1\venv\lib\site-packages\django\forms\forms.py", line 172, in errors
self.full_clean()
File "C:\Users\Desktop\real\django3.1\venv\lib\site-packages\django\forms\forms.py", line 376, in full_clean
self._post_clean()
File "C:\Users\Desktop\real\django3.1\venv\lib\site-packages\django\forms\models.py", line 411, in _post_clean
self.validate_unique()
File "C:\Users\Desktop\real\django3.1\venv\lib\site-packages\django\forms\models.py", line 420, in validate_unique
self.instance.validate_unique(exclude=exclude)
File "C:\Users\Desktop\real\django3.1\venv\lib\site-packages\django\db\models\base.py", line 1007, in validate_unique
date_errors = self._perform_date_checks(date_checks)
File "C:\Users\Desktop\real\django3.1\venv\lib\site-packages\django\db\models\base.py", line 1124, in _perform_date_checks
date = getattr(self, unique_for)
TypeError: getattr(): attribute name must be string
当我使用 py manage.py shell
创建时没有问题,但当我使用管理员界面时出现错误。
英文:
My models.py file:
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)
When use admin interface to add I get:
> TypeError at
> /admin/main/simple/add/
>
> getattr(): attribute name must be string
This is my errors in cmd:
Traceback (most recent call last):
File "C:\Users\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\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\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\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\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\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\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\Desktop\real\django3.1\venv\lib\site-packages\django\forms\fo
rms.py", line 172, in errors
self.full_clean()
File "C:\Users\Desktop\real\django3.1\venv\lib\site-packages\django\forms\fo
rms.py", line 376, in full_clean
self._post_clean()
File "C:\Users\Desktop\real\django3.1\venv\lib\site-packages\django\forms\mo
dels.py", line 411, in _post_clean
self.validate_unique()
File "C:\Users\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\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\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
When I use py manage.py shell
to create there is no problem, but when I use the admin interface I get errros.
答案1
得分: 2
你没有正确设置选项的默认值 default= Inner.b
更改为:
size = models.TextField(choices=Inner.choices, default=Inner.b, db_index=True)
英文:
You are not setting the default value for the choices properly default= Inner.b
change :
size = models.TextField(choices=Inner.choices,default='medium',
db_index=True)
to :
size = models.TextField(choices=Inner.choices,default= Inner.b,
db_index=True)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论