0

这是我的代码:

模型.py

class TblUser(models.Model):
    uid = models.IntegerField(primary_key=True, db_column='ID') # Field name made lowercase.
    username = models.CharField(max_length=3072, db_column='UserName', blank=True) # Field name made lowercase.
    password = models.CharField(max_length=3072, db_column='PassWord', blank=True) # Field name made lowercase.
    datesstart = models.DateTimeField(null=True, db_column='datesStart', blank=True) # Field name made lowercase.
    datesend = models.DateTimeField(null=True, db_column='datesEnd', blank=True) # Field name made lowercase.
    num = models.IntegerField(null=True, db_column='Num', blank=True) # Field name made lowercase.
    power = models.IntegerField(null=True, db_column='Power', blank=True) # Field name made lowercase.
    email = models.CharField(max_length=12288, blank=True)
    class Meta:
        db_table = u'tbl_user'

    def __unicode__(self):
        return '%d--%s--%d'%(self.uid,self.username,self.power)

视图.py

from app.models import TblUser
def appendUser(self,name,pwd):
    #I add a new user,and the primary_key:uid is autoincrement in datebase
    user = TblUser.objects.create(username=name,password=pwd)
    print user.uid#None

当我调用appendUser()时,它会在数据库中插入一条新记录,并且用户(TblUser 的实例)只有两个有效字段(用户名、密码),另一个为空。

我怎样才能得到,user.uid因为我想通过使用它来处理其他事情?

4

2 回答 2

2

您应该为 pk使用AutoFieldnot an : https ://docs.djangoproject.com/en/1.4/ref/models/fields/#autofieldIntegerField

于 2012-06-21T08:13:38.487 回答
0

您可能想阅读这篇文章,使用 ModelForm 的不同方法:

在 Django 中保存 ModelForm 后获取主键

于 2012-06-21T10:08:52.293 回答