为什么CreateView中的self.object在保存到数据库后有一个id? [英] Why doesn't self.object in a CreateView have an id after saving to the database?

查看:129
本文介绍了为什么CreateView中的self.object在保存到数据库后有一个id?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

django createview如何获取创建的对象,我试图使用由 CreateView 在其 get_success_url 方法。但是,尽管它绝对被保存到MySQL并接收一个ID,但是当我访问 self.object 时,它没有使用id。该模型确实有一个 id 属性。为什么我无法访问ID?如果我被链接的评论误入歧途,那么获得ID的正确方法是什么?



参考代码:



models.py

 从django.db导入模型

class User(models.Model):
id = models.IntegerField(primary_key = True)
name = models.CharField(max_length = 128)
active = models.BooleanField = true)
created_date = models.DateTimeField()
class Meta:
managed = False
db_table ='user'
def __unicode __(self):
return self.name

views.py

$ b从django.views.generic导入CreateB


$ b从django.core.urlresolvers import import
从表单导入AddUserForm

class AddUserView(CreateView):
form_class = AddUserForm
fields = ['name','active']#id和created_date在保存
tem时自动填充print_name ='webApp / add_user_form.html'

def get_success_url(self):
print self.object#打印提交的用户的名称
print self.object.id#打印无
返回反向(webApp:user:stepTwo,args =(self.object.id,))


解决方案

它是空白的,因为对象实际上没有接收到一个id。这是因为 id 字段应该是一个 AutoField ,而不是IntegerField,因此它被声明为自动递增。



实际上,你根本不应该声明它:主键AutoField命名的id是默认的< a>,所以你应该把它留下来。



注意,在进行更改后,您需要迁移数据库表,或者删除并重新创建它。 p>

Following the comment on the accepted answer on django createview how to get the object that is created, I am attempting to use the id from a user created by a CreateView in its get_success_url method. However, even though it is definitely being saved to MySQL and receiving an id, when I access self.object, it doesn't have an id to use. The model does have an id property. Why wouldn't I be able to access the id? If I'm being led astray by the linked comment, what is the right way to get the id?

Reference code:

models.py

from django.db import models

class User(models.Model):
    id = models.IntegerField(primary_key=True)
    name = models.CharField(max_length=128)
    active = models.BooleanField(blank=True)
    created_date = models.DateTimeField()
    class Meta:
        managed = False
        db_table = 'user'
    def __unicode__(self):
        return self.name

views.py

from django.views.generic import CreateView
from django.core.urlresolvers import reverse
from forms import AddUserForm

class AddUserView(CreateView):
    form_class = AddUserForm
    fields = ['name', 'active'] # id and created_date are auto-filled upon save
    template_name = 'webApp/add_user_form.html'

    def get_success_url(self):
        print self.object # Prints the name of the submitted user
        print self.object.id # Prints None
        return reverse("webApp:user:stepTwo", args=(self.object.id,))

解决方案

It's blank because the object is not actually receiving an id. That's because the id field should be an AutoField, not IntegerField, so that it is declared as auto-incrementing.

Actually though you should not declare it at all: an primary key AutoField named id is the default, so you should leave it out.

Note you'll need to migrate your database table, or drop and recreate it, after you make this change.

这篇关于为什么CreateView中的self.object在保存到数据库后有一个id?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆