2016-06-18 8 views
0

は私がページにジャンゴNonetypeオブジェクトには、属性「ID」

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ある

モデル:

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)... 

答えて

1

djangoモデルの場合、 "increment"フィールドの名前が "id"のデフォルトフィールドがあります。あなたが明示的にidを提供しなければならないので、あなたはproduct object

より良いソリューションを作成するたびに、あなたのID次にAutoFieldからidIntegerField

id = models.AutoField(primary_key=True) 

を変更することですid = models.IntegerField(primary_key=True)

あるIntegerFieldでそのIDをオーバーライドしています新しいオブジェクトを作成するたびにidを渡す必要はありません。

2

djangoのORMで、idフィールドの主キーなどに使用されるauthoフィールドまたはserialフィールドを作成するために、整数フィールドの代わりに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)... 
関連する問題