django模型不返回主键,为什么主键是none

时间:2015-08-12 05:09:06

标签: python django django-models django-orm

我的代码:

broadcast_obj = TblBroadcast.objects.using('broadcast_db').create(
                                name=request.POST.get('name'),
                                summary=request.POST.get('summary', ''),
                                cover=request.POST.get('cover'),
                                status=int(request.POST.get('status')),
                                quality=int(request.POST.get('quality'))
                                )
print "========="
print broadcast_obj.name
print broadcast_obj.summary
print broadcast_obj.cover
print broadcast_obj.status
print broadcast_obj.pk
print broadcast_obj.quality
print "========="

输出:

=========
t
t
t
0
None
6
=========

为什么主键不是?当我检查数据库时,django已经创建了项目。broadcast_idpk,它是auto_incremententer image description here

1 个答案:

答案 0 :(得分:3)

您的型号是否有主键?

class TblBroadcast(models.Model)
    broadcast_id = models.AutoField(primary_key=True)

请参阅下文 https://docs.djangoproject.com/en/1.8/topics/db/models/#automatic-primary-key-fields