python 带有 ImageField 的模型的管理面板中的“getattr():属性名称必须是字符串”错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1683362/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
'getattr(): attribute name must be string' error in admin panel for a model with an ImageField
提问by cwj
I have the following model set up:
我设置了以下模型:
class UserProfile(models.Model):
"Additional attributes for users."
url = models.URLField()
location = models.CharField(max_length=100)
user = models.ForeignKey(User, unique=True)
avatar = models.ImageField(upload_to='/home/something/www/avatars', height_field=80, width_field=80)
def __unicode__(self):
return "Profile of " + self.user.username
It's supposed to store additional information about a user, for example an avatar.
它应该存储有关用户的其他信息,例如avatar。
Unfortunately, when I try to upload an image via the admin panel, it gives me an error, something like:
不幸的是,当我尝试通过管理面板上传图像时,它给了我一个错误,例如:
getattr(): attribute name must be string
Which is not produced when I remove that field from the model, do a db reset and reload the server. I'd imagine the cause is this particular field, just not sure how.
当我从模型中删除该字段时不会产生这种情况,执行数据库重置并重新加载服务器。我想原因是这个特定的领域,只是不确定如何。
This is my traceback:
这是我的回溯:
File "/usr/lib/pymodules/python2.6/django/core/handlers/base.py" in get_response
92. response = callback(request, *callback_args, **callback_kwargs)
File "/usr/lib/pymodules/python2.6/django/contrib/admin/options.py" in wrapper
226. return self.admin_site.admin_view(view)(*args, **kwargs)
File "/usr/lib/pymodules/python2.6/django/views/decorators/cache.py" in _wrapped_view_func
44. response = view_func(request, *args, **kwargs)
File "/usr/lib/pymodules/python2.6/django/contrib/admin/sites.py" in inner
186. return view(request, *args, **kwargs)
File "/usr/lib/pymodules/python2.6/django/db/transaction.py" in _commit_on_success
240. res = func(*args, **kw)
File "/usr/lib/pymodules/python2.6/django/contrib/admin/options.py" in add_view
718. new_object = self.save_form(request, form, change=False)
File "/usr/lib/pymodules/python2.6/django/contrib/admin/options.py" in save_form
551. return form.save(commit=False)
File "/usr/lib/pymodules/python2.6/django/forms/models.py" in save
407. fail_message, commit, exclude=self._meta.exclude)
File "/usr/lib/pymodules/python2.6/django/forms/models.py" in save_instance
65. f.save_form_data(instance, cleaned_data[f.name])
File "/usr/lib/pymodules/python2.6/django/db/models/fields/files.py" in save_form_data
283. setattr(instance, self.name, data)
File "/usr/lib/pymodules/python2.6/django/db/models/fields/files.py" in __set__
316. self.field.update_dimension_fields(instance, force=True)
File "/usr/lib/pymodules/python2.6/django/db/models/fields/files.py" in update_dimension_fields
368. (self.width_field and not getattr(instance, self.width_field))
Exception Type: TypeError at /admin/proj/userprofile/add/
Exception Value: getattr(): attribute name must be string
回答by Tendayi Mawushe
Your problem is with height_field=80
and width_field=80
these should not contain the height and width you require but rather the names of fields in your model that can have the values for height and width save in them.
你的问题是height_field=80
和width_field=80
这些不应该包含的高度和宽度您需要,但你的模型字段可以有高度和宽度值保存在其中的相当的名字。
As explained in the Django documentation for the ImagedFieldthese are attributes on your model which will be populated for you when the model is saved. If you want this information populated for you the create model attribute where this information can be stored otherwise just remove these attributes they are optional.
正如ImagedField的 Django 文档中所解释的,这些是模型上的属性,将在保存模型时为您填充。如果您希望为您填充此信息,则可以在其中存储此信息的创建模型属性,否则只需删除这些属性,它们是可选的。
回答by Daniel Roseman
The problem is probably this:
问题大概是这样的:
height_field=80, width_field=80
height_field
and width_field
, if you use them, are supposed to be names of fields on the model which contain the height and width information. Fix this it should work.
height_field
and width_field
,如果你使用它们,应该是模型上包含高度和宽度信息的字段的名称。修复它应该可以工作。