我的代码:
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_id
是pk
,它是auto_increment
。
答案 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