Python django-不立即保存到数据库
另一个新手问题 在我将项目保存到数据库后,我立即尝试访问 它的主键用于重定向其页面。但我没能完成。 我尝试手动处理事务,如本文所述 这可能是因为使用了管理模式吗 我得到这个错误:Python django-不立即保存到数据库,python,django,Python,Django,另一个新手问题 在我将项目保存到数据库后,我立即尝试访问 它的主键用于重定向其页面。但我没能完成。 我尝试手动处理事务,如本文所述 这可能是因为使用了管理模式吗 我得到这个错误: invalid literal for int() with base 10: 'None' 我将返回行改为this,将id转换为string return HttpResponseRedirect("/blog/page/"+str(page.id)+"/") 这是代码段 @transaction.commi
invalid literal for int() with base 10: 'None'
我将返回行改为this,将id转换为string
return HttpResponseRedirect("/blog/page/"+str(page.id)+"/")
这是代码段
@transaction.commit_manually
def new_post_save(request):
.
.
.
page.save()
sid = transaction.savepoint()
transaction.savepoint_commit(sid)
return HttpResponseRedirect("/blog/page/"+page.id+"/")
以下是原始视图和模型的其余部分
def new_post_save(request):
page_name = request.POST["page_name"]
content = request.POST["content"]
postCategory = request.POST["cat"]
page = BlogPost(title = page_name,body = content, author = request.user, category = postCategory)
page.save()
return HttpResponseRedirect("/blog/page/"+page.id+"/")
模型
def new_post_save(request):
page_name = request.POST["page_name"]
content = request.POST["content"]
postCategory = request.POST["cat"]
page = BlogPost(title = page_name,body = content, author = request.user, category = postCategory)
page.save()
return HttpResponseRedirect("/blog/page/"+page.id+"/")
class BlogPost(models.Model):
id = models.IntegerField(primary_key=True)
author = models.ForeignKey(User)
title = models.CharField(max_length=128)
body = models.TextField()
category = models.CharField(max_length=10, default='other')
def __unicode__(self):
return self.title
这里base.py我想我没有重写save函数
def save(self, force_insert=False, force_update=False, using=None):
"""
Saves the current instance. Override this in a subclass if you want to
control the saving process.
The 'force_insert' and 'force_update' parameters can be used to insist
that the "save" must be an SQL insert or update (or equivalent for
non-SQL backends), respectively. Normally, they should not be set.
"""
if force_insert and force_update:
raise ValueError("Cannot force both insert and updating in model saving.")
self.save_base(using=using, force_insert=force_insert, force_update=force_update)
save.alters_data = True
按数据库在settings.py中
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': 'blog.db',
'USER': '',
'PASSWORD': '',
'HOST': '',
'PORT': '',
}
}
使用此选项,而不是手动调用save() page=BlogPost.objects.create(title=page\u name,body=content, 作者=请求。用户,类别=后类别)
从模型类中删除
id
字段
如果您不指定主键,Django将自动插入一个名为id
的自动字段,因此您不需要它
因为您特别说过您的
id
字段是一个整数主键,Django希望您自己管理它。正如您所声明的,它是一个IntField
,不是一个AutoField
,因此它不会自动分配任何值。这里不需要对事务执行任何操作。您的原始代码是什么?尝试旧方法和新方法时出现了哪些错误?在调试模式下,当我到达返回行时,page.id为None。旧方法和新方法之间没有区别,因为page.id都是none。如果对象真的保存了,它会有一个可以立即访问的id。您是否碰巧覆盖了save
方法?由于某种原因,该对象实际上没有被保存,但是如果没有更多的代码,就不可能知道原因。@ChrisPratt谢谢,我在上面添加了保存函数。create也不起作用。与save()相同,它会添加到数据库中,但不会立即添加。谢谢,我会尝试,但有另一个问题。如果我删除id字段,如何使用blogpost对象的主键重定向?如果在模型定义中不包含主键字段,Django将添加一个。它将被称为“id”,是一个自动递增的整数字段。