简体   繁体   English

如何将模型的实例传递给 Django 中的另一个模型?

[英]How to pass instance of a model to another model in Django?

I'm trying to populate dbkey column from parent table on the form, where foreign table values are input.我正在尝试从表单上的父表填充 dbkey 列,其中输入了外部表值。 WHile providing a value I get the following error - "csg_ch_servers.DBParentCustomerKey" must be a "csg_ch_customer" instance.在提供值时,我收到以下错误 - “csg_ch_servers.DBParentCustomerKey”必须是“csg_ch_customer”实例。 Could someone help me out?有人可以帮我吗?

model.py模型.py

class csg_ch_customer(models.Model):
    DBKey=models.AutoField(primary_key=True)
    CustomerName=models.CharField(max_length=100,unique=True)
    class Meta:
        db_table="csg_ch_customer"
    def  __unicode__(self):
        return self.DBKey
    def __init__(self):
        return self.DBKey

class csg_ch_servers(models.Model):
    DBKey=models.AutoField(primary_key=True)
    DBParentCustomerKey=models.ForeignKey(csg_ch_customer,on_delete=models.CASCADE)
    class Meta:
        db_table="csg_ch_servers"
    def __str__(self):
        return self.DBParentCustomerKey
    def __unicode__(self):
        return self.DBParentCustomerKey

form.py表单.py

class csg_ch_customer_form(forms.ModelForm):

    class Meta:
        model  = models.csg_ch_customer
        fields = "__all__"

class csg_ch_servers_form(forms.ModelForm):

    class Meta:
        model  = models.csg_ch_servers
        fields = "__all__"

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields['DBParentCustomerKey'].queryset = models.csg_ch_customer.objects.values_list('DBKey', flat=True)

views.py视图.py

def register_ser(request):
    form=Aforms.csg_ch_servers_form()
    if request.method == "POST":
        form=Aforms.csg_ch_servers_form(request.POST)
        if form.is_valid():
            form.save()
            return redirect('Admin/NewServer.html')
    else:
        return render(request,'Admin/NewServer.html',{'form':form})
    return render(request,'Admin/NewServer.html',{'form':form})

Your form needs to instanciate your model in your views.您的表单需要在您的视图中实例化您的模型。 I added few fields to your view, but you need to edit it for your own purpose.我在您的视图中添加了几个字段,但您需要根据自己的目的对其进行编辑。

This should be as simple as possible.这应该尽可能简单。

def register_ser(request,pk):
    yourmodel = models.csg_ch_customer.objects.get(pk=pk)
    form=Aforms.csg_ch_servers_form(instance=yourmodel)
    if request.method == "POST":
        form=Aforms.csg_ch_servers_form(request.POST,instance=yourmodel)
        if form.is_valid():
            form.save()
            return redirect('Admin/NewServer.html')
    else:
        return render(request,'Admin/NewServer.html',{'form':form})
    return render(request,'Admin/NewServer.html',{'form':form})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在django中的另一个模型保存方法中更新模型实例? - How to update a model instance in another model save method in django? 如何基于Django中的属性值将模型实例复制到另一个模型 - How to copy a model instance to another model based on attribute value in Django 如何基于另一个 model 的实例在 Django 中创建 model,但经过过滤 - How to create a model in Django based on the instance of another model, but filtered 将Django模型实例保存到另一个模型中 - Saving django model instance into another model 在创建另一个Django模型实例的同时创建一个Django模型实例 - Create a Django model instance concurrent with the creation of another Django model instance Django:如何在每次更新另一个模型实例时自动更新模型实例 - Django: How to automatically update a model instance everytime another model instance is updated 如何将模型实例与 Django 中的另一个实例相关联? - How can I relate a model instance to another in django? 如何在django中连接模型实例 - how to connect model instance in django 如何修复模型的 Django 实例 - how to fix django instance of a model Django删除另一个模型中没有更多相关外键项的模型实例? - Django deleting a model instance with no more related ForeignKey items in another model?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM