Django ValueError:无法按模型分配必须是实例
我有两个模型 Tax_settings 和 Item_Creation 我希望使用 Tax_settings 中的 tax_id 作为 Item_creation 中的 tax_code。我已经使用 tax_code 作为外键。现在我正在模板中对其执行操作,以获取tax_code列表。但出现此错误
models.py
class Tax_Settings(models.Model):
tax_id = models.IntegerField()
name = models.CharField(max_length=50)
tax_description = models.CharField(max_length=50)
class Meta:
verbose_name = "tax_id"
def __str__(self):
return self.tax_id
class Item_Creation(models.Model):
item=models.CharField(max_length=50)
hsn=models.IntegerField()
item_code = models.IntegerField()
item_description = models.CharField(max_length=50)
unit = models.IntegerField()
tax_code = models.ForeignKey(Tax_Settings,on_delete=models.CASCADE)
views.py
def item_creation(request):
get_tax_code=Tax_Settings.objects.values_list('tax_id')[0]
print(get_tax_code)
if request.method == 'POST':
item = request.POST['item']
hsn = request.POST['hsn']
item_code=request.POST['item_code']
item_description=request.POST['item_description']
unit=request.POST['unit']
tax_code = request.POST['tax_code']
item_details = Item_Creation.objects.create(item=item, hsn=hsn, item_code=item_code,item_description=item_description, unit=unit, tax_code=tax_code)
item_details.save()
print("item_details",item_details)
return render(request,"settings/item-creation.html")
return render(request,"settings/item-creation.html",{'get_tax':get_tax_code})
item-creation.html
<div class="col-md mb-2">
<select id="tax-code" class="form-control" name="tax_code" >
<option value="">Select Tax Code</option>
{% if get_tax %}
{% for t in get_tax %}
<option value="{{t}}">{{t}}</option>
{% endfor %}
{% endif %}
</select>
I have two models Tax_settings and Item_Creation
I wish to use tax_id from Tax_settings as tax_code in Item_creation. I already use tax_code as a foreign key. And now I am performing a actions on it in templates to get a list of tax_code. But getting this error
models.py
class Tax_Settings(models.Model):
tax_id = models.IntegerField()
name = models.CharField(max_length=50)
tax_description = models.CharField(max_length=50)
class Meta:
verbose_name = "tax_id"
def __str__(self):
return self.tax_id
class Item_Creation(models.Model):
item=models.CharField(max_length=50)
hsn=models.IntegerField()
item_code = models.IntegerField()
item_description = models.CharField(max_length=50)
unit = models.IntegerField()
tax_code = models.ForeignKey(Tax_Settings,on_delete=models.CASCADE)
views.py
def item_creation(request):
get_tax_code=Tax_Settings.objects.values_list('tax_id')[0]
print(get_tax_code)
if request.method == 'POST':
item = request.POST['item']
hsn = request.POST['hsn']
item_code=request.POST['item_code']
item_description=request.POST['item_description']
unit=request.POST['unit']
tax_code = request.POST['tax_code']
item_details = Item_Creation.objects.create(item=item, hsn=hsn, item_code=item_code,item_description=item_description, unit=unit, tax_code=tax_code)
item_details.save()
print("item_details",item_details)
return render(request,"settings/item-creation.html")
return render(request,"settings/item-creation.html",{'get_tax':get_tax_code})
item-creation.html
<div class="col-md mb-2">
<select id="tax-code" class="form-control" name="tax_code" >
<option value="">Select Tax Code</option>
{% if get_tax %}
{% for t in get_tax %}
<option value="{{t}}">{{t}}</option>
{% endfor %}
{% endif %}
</select>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这里的问题是你必须将tax_id作为主键,除非它将采用“id”(这是自动创建的主键,因为你没有创建主键)。
将您的
tax_id
编辑为tax_id = models.IntegerField(primary_key=True)
并重新迁移,然后根据以下内容编辑您的视图
Here the problem is your have to make your tax_id as your primary key unless it will take the 'id' (which is primary key automatically created because you did not create a primary key).
Edit your
tax_id
totax_id = models.IntegerField(primary_key=True)
and re migrateThen edit your views based on below