django:如何从包含外键的多个模型中创建一个表单 [英] django: How to make one form from multiple models containing foreignkeys

查看:135
本文介绍了django:如何从包含外键的多个模型中创建一个表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在一个使用多个模型的页面上创建表单。模型参考。我无法获取表单进行验证,因为我无法弄清楚如何获取表单中使用的两个模型的ID,以验证它。我在模板中使用了一个隐藏的键,但是我无法弄清楚如何使其在视图中工作。



我的代码如下:



视图:

  def the_view(request,a_id,):

if request.method =='POST':

b_form = BForm(request.POST)
c_form = CForm(request.POST)
打印post
if b_form.is_valid()和c_form.is_valid():
打印有效
b_form.save()
c_form.save()
返回HttpResponseRedirect(reverse('myproj。 pro.views.this_page'))
else:
b_form = BForm()
c_form = CForm()
b_ide = B.objects.get(pk = request.b_id)
id_of_a = A.objects.get(pk = a_id)
返回render_to_response('myproj / a / c.html',
{'b_form':b_form,
'c_form ':c_form,
'id_of_a':id_of_a,
'b_id':b_ide})






型号

  clas s A(models.Model):
name = models.CharField(max_length = 256,null = True,blank = True)
classe = models.CharField(max_length = 256,null = True,blank =真的)

def __str __(self):
return self.name


class B(models.Model):

aid = models.ForeignKey(A,null = True,blank = True)
number = models.IntegerField(max_length = 1000)
other_number = models.IntegerField(max_length = 1000)


class C(models.Model):
bid = models.ForeignKey(B,null = False,blank = False)
field_name = models.CharField(max_length = 15)
field_value = models.CharField(max_length = 256,null = True,blank = True)

表单

  from mappamundi.mappa.models import A,B,C 


class BForm(forms.ModelForm):
class Meta:
model = B
exclude =('aid',)

class CForm(forms.ModelForm):
class Meta:
mo del = C
exclude =('bid',)






B有一个外键引用A,C有一个外键引用B.由于模型是相关的,我想在一个页面上提供它们的表单,1个提交按钮。由于我需要填写B和C&我不想从下拉列表中选择B的ID,我需要以某种方式获取B表单的ID,以便它将验证。我在模板中有一个隐藏的字段,我只需要看看如何在视图中执行

解决方案

你有代码几乎是对的只需执行以下操作:

 如果b_form.is_valid()和c_form.is_valid():
打印有效
b = b_form.save()
c = c_form.save(commit = False)
cb = b
c.save()
pre>

I am trying to make a form on one page that uses multiple models. The models reference each other. I am having trouble getting the form to validate because I cant figure out how to get the id of two of the models used in the form into the form to validate it. I used a hidden key in the template but I cant figure out how to make it work in the views

My code is below:

views:

def the_view(request, a_id,):

  if request.method == 'POST':

     b_form= BForm(request.POST)
     c_form =CForm(request.POST)
     print "post"
     if b_form.is_valid() and c_form.is_valid():
        print "valid"
        b_form.save()
        c_form.save()
        return HttpResponseRedirect(reverse('myproj.pro.views.this_page'))
  else:
     b_form= BForm()
     c_form = CForm()
     b_ide = B.objects.get(pk=request.b_id)
     id_of_a = A.objects.get(pk=a_id)
  return render_to_response('myproj/a/c.html', 
{'b_form':b_form, 
 'c_form':c_form, 
 'id_of_a':id_of_a, 
  'b_id':b_ide     })


models

class A(models.Model):
    name = models.CharField(max_length=256, null=True, blank=True)
    classe = models.CharField(max_length=256, null=True, blank=True)

   def __str__(self):
      return self.name


class B(models.Model):

    aid = models.ForeignKey(A, null=True, blank=True)
    number =  models.IntegerField(max_length=1000)
    other_number =  models.IntegerField(max_length=1000)


class C(models.Model):
   bid = models.ForeignKey(B, null=False, blank=False)
   field_name = models.CharField(max_length=15)
   field_value = models.CharField(max_length=256, null=True, blank=True)


forms

from mappamundi.mappa.models import A, B, C


class BForm(forms.ModelForm):
   class Meta:
     model = B
     exclude = ('aid',)

class CForm(forms.ModelForm):
   class Meta:
     model = C
     exclude = ('bid',)


B has a foreign key reference to A, C has a foreign key reference to B. Since the models are related, I want to have the forms for them on one page, 1 submit button. Since I need to fill out fields for the forms for B and C & I dont want to select the id of B from a drop down list, I need to somehow get the id of the B form into the form so it will validate. I have a hidden field in the template, I just need to figure how to do it in the views

解决方案

The code you have is almost right. Just do:

if b_form.is_valid() and c_form.is_valid():
    print "valid"
    b = b_form.save()
    c = c_form.save(commit=False)
    c.b = b
    c.save()

这篇关于django:如何从包含外键的多个模型中创建一个表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆