我想创建产品元素并重定向到他的页面
def newprodcreate(request, c_id):
if models.company.objects.get(email = request.user.username).id == int(c_id):
name = request.POST['newprodname']
comp = models.company.objects.get(id = int(c_id))
prod = models.product()
prod.name = name
prod.comp_id = int(c_id)
prod.address = comp.address
prod.lat = comp.lat
prod.lng = comp.lng
prod.phone = comp.phone
prod.cur_id = 2
prod.save()
return HttpResponseRedirect("/p/" + str(prod.id))
在数据库中创建的元素,但prod.id为Null
模型:
class product(models.Model):
class Meta:
db_table = "product"
id = models.IntegerField(primary_key=True)
crdate = models.DateTimeField(default = datetime.now())
comp_id = models.IntegerField()
categ = models.CharField(max_length=200, default="")
img = models.FileField(upload_to=MEDIA_ROOT +"/product/", max_length=200)
name = models.CharField(max_length=200)...
答案 0 :(得分:2)
亲爱的michael在django ORM中用于创建用于id主键等的autho字段或串行字段,我们使用Autofield而不是整数字段。
id = models.AutoField(primary_key=True)
your model in corrected state:
class product(models.Model):
class Meta:
db_table = "product"
id = models.AutoField(primary_key=True)
crdate = models.DateTimeField(default =
datetime.now())
comp_id = models.IntegerField()
categ = models.CharField(max_length=200, default="")
img = models.FileField(upload_to=MEDIA_ROOT. +"/product/", max_length=200)
name = models.CharField(max_length=200)...
答案 1 :(得分:1)
对于django模型将有一个名为“id”的默认字段,它是自动增量字段。您已使用this
var numbers = ['1', 23, null, 46.5, '34e2', , false, 'true', , 40],
divide = numbers.map(x => 'string' === typeof x && isFinite(x) ? x/10 : x);
console.log(divide);
覆盖该ID
因此,每次创建IntegerField
id = models.IntegerField(primary_key=True)
更好的解决方案是将id
product object
更改为id
IntegerField
然后,您的ID将自动创建,无需在每次创建新对象时传递id。