AutoField无法在Django-nonrel中运行

时间:2014-07-20 16:46:20

标签: django django-nonrel

型号:

class MasterDishList(models.Model):
    dishid = models.AutoField(primary_key=True)
    dishname = models.CharField(max_length=100)
    username = models.ForeignKey(User)
    dishprice = models.IntegerField()

如果我尝试:

objectoadd = MasterDishList.objects.create(dishname="somename", dishprice=100, username=userobject)

我明白了:

/home/abd/Development/Projects/misc/fadster-backend/djangotoolbox/db/basecompiler.pyc in execute_sql(self, return_id)
    581                 if value is None and not field.null and not field.primary_key:
    582                     raise IntegrityError("You can't set %s (a non-nullable "
--> 583                                          "field) to None!" % field.name)
    584 
    585                 # Prepare value for database, note that query.values have

IntegrityError: You can't set dishid (a non-nullable field) to None!

这显然是个错误吗?或者我错过了什么? 我可以使用的任何解决方法吗?

1 个答案:

答案 0 :(得分:0)

您的主键似乎与默认键相同。建议不要指定字段,因为它将由django自动添加:

Automatic primary key fields

您可以将模型保留为:

class MasterDishList(models.Model):
    dishname = models.CharField(max_length=100)
    username = models.ForeignKey(User)
    dishprice = models.IntegerField()