在包含ImageField的模型的管理面板中出现'getattr(): 属性名必须为字符串'错误
我有一个这样的模型设置:
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
这个模型是用来存储用户的额外信息,比如一个头像。
不幸的是,当我试图通过管理面板上传图片时,出现了一个错误,类似于:
getattr(): attribute name must be string
当我把这个字段从模型中去掉,重置数据库并重新加载服务器时,就不会出现这个错误。我想这个问题可能和这个特定的字段有关,但我不太确定具体是什么原因。
这是我的错误追踪信息:
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
3 个回答
0
基本上,
width_field and height_field
是Django用来指代模型字段的,这个字段将用来存储图片的宽度和高度的值。
你可以试试这个;
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="img_height", width_field="img_height")
img_width = models.PositiveIntegerField()
img_height = models.PositiveIntegerField()
def __str__(self):
return "Profile of " + self.user.username
9
问题可能出在这里:
height_field=80, width_field=80
height_field
和 width_field
如果你使用了它们,应该是模型中用来存储高度和宽度信息的字段名称。修正这个问题后应该就能正常工作了。
23
你的问题出在 height_field=80
和 width_field=80
这两个地方。它们不应该直接写你想要的高度和宽度,而是应该写你模型中可以存储高度和宽度值的字段名称。
正如Django文档中对 ImagedField 的解释,这些是你模型上的属性,当模型被保存时,这些属性会自动填充。如果你希望这些信息能够自动填充,就需要创建一个模型属性来存储这些信息;如果不需要,可以直接去掉这些属性,它们是可选的。