我有一个关于产品的模型
models.py
...
class ProductsTbl(models.Model):
....
description = models.TextField(blank=True, null=True)
user = models.ForeignKey(User, blank=True, null=True)
useredit = models.CharField(max_length=32, blank=True, null=True)
image = models.ImageField(upload_to=get_imagep_Product, blank=True)
def __unicode__(self):
return self.name
def save(self, *args, **kwargs):
''' On save, update timestamps '''
if not self.id:
self.created = timezone.now()
return super(ProductsTbl, self).save(*args, **kwargs)
如果我修改了描述或新图像等数据后,等等。我想数据useredit写了编辑用户名,
这是
中的编辑事物功能views.py
....
@login_required
def edit_thing(request, slug):
# grab the object...
thing = ProductsTbl.objects.get(slug=slug)
if thing.user != request.user:
raise Http404
# set the form we're using...
form_class = ProductsTblForm
if request.method == 'POST':
# grab the data from the submitted form
form = form_class(data=request.POST,files=request.FILES,instance=thing)
if form.is_valid():
# create an instance but do not save yet
thing = form.save(commit=False)
# set the additional details
thing.useredit = request.user
# save the object
thing.save()
return redirect('thing_detail', slug=thing.slug)
# otherwise just create the form
else:
form = form_class(instance=thing)
# and render the template
return render(request, 'things/edit_thing.html', {
'thing': thing,
'form': form,
})
....
但是,当我修改了东西信息时,只显示下面的错误,,,。
Environment:
Request Method: POST
Request URL: http://127.0.0.1:8000/things/md0013/edit/
Django Version: 1.8.11
Python Version: 2.7.11
Installed Applications:
('designer',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'registration')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware')
Traceback:
File "/Users/korekyourin/books/helloApp/venv/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
132. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/korekyourin/books/helloApp/venv/lib/python2.7/site-packages/django/contrib/auth/decorators.py" in _wrapped_view
22. return view_func(request, *args, **kwargs)
File "/Users/korekyourin/books/stayreal/sandbox/product_id/designer/views.py" in edit_thing
52. thing.save()
File "/Users/korekyourin/books/stayreal/sandbox/product_id/designer/models.py" in save
60. return super(ProductsTbl, self).save(*args, **kwargs)
File "/Users/korekyourin/books/helloApp/venv/lib/python2.7/site-packages/django/db/models/base.py" in save
734. force_update=force_update, update_fields=update_fields)
File "/Users/korekyourin/books/helloApp/venv/lib/python2.7/site-packages/django/db/models/base.py" in save_base
762. updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "/Users/korekyourin/books/helloApp/venv/lib/python2.7/site-packages/django/db/models/base.py" in _save_table
827. forced_update)
File "/Users/korekyourin/books/helloApp/venv/lib/python2.7/site-packages/django/db/models/base.py" in _do_update
877. return filtered._update(values) > 0
File "/Users/korekyourin/books/helloApp/venv/lib/python2.7/site-packages/django/db/models/query.py" in _update
580. return query.get_compiler(self.db).execute_sql(CURSOR)
File "/Users/korekyourin/books/helloApp/venv/lib/python2.7/site-packages/django/db/models/sql/compiler.py" in execute_sql
1062. cursor = super(SQLUpdateCompiler, self).execute_sql(result_type)
File "/Users/korekyourin/books/helloApp/venv/lib/python2.7/site-packages/django/db/models/sql/compiler.py" in execute_sql
829. sql, params = self.as_sql()
File "/Users/korekyourin/books/helloApp/venv/lib/python2.7/site-packages/django/db/models/sql/compiler.py" in as_sql
1028. (field.__class__.__name__, val.__class__.__name__))
Exception Type: TypeError at /things/md0013/edit/
Exception Value: Database is trying to update a relational field of type CharField with a value of type User. Make sure you are setting the correct relations
如何在数据库中编写编辑器名称?谢谢
答案 0 :(得分:1)
在您的views.py中将thing.useredit = request.user
更改为thing.useredit = request.user.username