Django ValueError:无法按模型分配必须是实例

发布于 2025-01-17 02:14:29 字数 2415 浏览 0 评论 0原文

我有两个模型 Tax_settingsItem_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>

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怕倦 2025-01-24 02:14:29

这里的问题是你必须将tax_id作为主键,除非它将采用“id”(这是自动创建的主键,因为你没有创建主键)。

将您的 tax_id 编辑为 tax_id = models.IntegerField(primary_key=True) 并重新迁移

,然后根据以下内容编辑您的视图

tax_code = request.POST['tax_code']
tax_obj = Tax_Settings.objects.get(tax_id=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_obj)
item_details.save()

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 to tax_id = models.IntegerField(primary_key=True) and re migrate

Then edit your views based on below

tax_code = request.POST['tax_code']
tax_obj = Tax_Settings.objects.get(tax_id=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_obj)
item_details.save()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文