cwj*_*cwj 9 python django django-models django-admin
我有以下模型设置:
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
Run Code Online (Sandbox Code Playgroud)
它应该存储关于用户的附加信息,例如化身.
不幸的是,当我尝试通过管理面板上传图片时,它会给我一个错误,例如:
getattr(): attribute name must be string
Run Code Online (Sandbox Code Playgroud)
当我从模型中删除该字段时,不生成哪个,执行db重置并重新加载服务器.我想原因是这个特殊的领域,只是不确定如何.
这是我的追溯:
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
Run Code Online (Sandbox Code Playgroud)
Ten*_*she 18
你的问题是height_field=80和width_field=80这些不应该包含的高度和宽度您需要,但你的模型字段可以有高度和宽度值保存在其中的相当的名字.
正如ImagedField的Django文档中所解释的那样,这些是模型上的属性,将在保存模型时为您填充.如果您希望为您填充此信息,则可以存储此信息的创建模型属性,否则只需删除这些属性,它们是可选的.
问题可能是这样的:
height_field=80, width_field=80
Run Code Online (Sandbox Code Playgroud)
height_field并且width_field,如果您使用它们,则应该是模型中包含高度和宽度信息的字段的名称.解决这个问题它应该工作.
| 归档时间: |
|
| 查看次数: |
14176 次 |
| 最近记录: |