在 Django class-base CreateView 内部,如何初始化中间模型
Inside Django class-base CreateView, how to init intermediate model
我有三个模型,PiItem是"through"的Pi和Items模型的接口。
现在我想在 Django CreatedView class 中用新的 Pi 实例和新的 PiItem 实例创建一个新的 Items 实例。
不知何故,我不断收到错误消息
*
*****MultipleObjectsReturned at /product/ get() returned more than one PiItem*****
*
任何帮助将不胜感激
谢谢
这是模型
class Pi(models.Model):
company = models.ForeignKey(CompanyProfile, related_name='pi', null=True)
contact = models.ForeignKey(Contact, null=True)
def __unicode__(self):
#return u'%s, %s' %(self.company.companyprofile_name, self.reference_id)
return u'%s' %(self.company.companyprofile_name)
class Items(models.Model):
product_id = models.CharField("Product ID",max_length=50, blank=False, null=True, unique=True)
in_pi = models.ManyToManyField(Pi, through='PiItem', blank=True)
def __unicode__(self):
return self.product_id
class PiItem(models.Model):
item_name= models.ForeignKey(Items)
pi = models.ForeignKey(Pi)
def __unicode__(self):
return self.pi.reference_id
这是我的 view.py
from django.views.generic.edit import CreateView
from pi.models import Items, PiItem, Pi
from django.shortcuts import get_object_or_404
class AddItemView(CreateView):
context_object_name = 'Product_list'
model = Items
template_name = "product.html"
fields = "__all__"
def get_initial(self):
in_pi = get_object_or_404(PiItem)
return {
'in_pi':in_pi
}
这是模板
{% extends 'base.html' %}
{% load crispy_forms_tags %}
{% block pi %}
<form action="" method="post">{% csrf_token %}
{{ form|crispy }}
{{ form.get_form_class() }}
<input type="submit" value="Create" />
</form>
问题最有可能出在这里:
in_pi = get_object_or_404(PiItem)
您需要通过向每个相关模型传递额外的参数(例如外键)来过滤 PiItem 对象,例如:
in_pi = get_object_or_404(PiItem, item_name=something1, pi=something2)
我有三个模型,PiItem是"through"的Pi和Items模型的接口。
现在我想在 Django CreatedView class 中用新的 Pi 实例和新的 PiItem 实例创建一个新的 Items 实例。
不知何故,我不断收到错误消息
*
*****MultipleObjectsReturned at /product/ get() returned more than one PiItem*****
*
任何帮助将不胜感激 谢谢
这是模型
class Pi(models.Model):
company = models.ForeignKey(CompanyProfile, related_name='pi', null=True)
contact = models.ForeignKey(Contact, null=True)
def __unicode__(self):
#return u'%s, %s' %(self.company.companyprofile_name, self.reference_id)
return u'%s' %(self.company.companyprofile_name)
class Items(models.Model):
product_id = models.CharField("Product ID",max_length=50, blank=False, null=True, unique=True)
in_pi = models.ManyToManyField(Pi, through='PiItem', blank=True)
def __unicode__(self):
return self.product_id
class PiItem(models.Model):
item_name= models.ForeignKey(Items)
pi = models.ForeignKey(Pi)
def __unicode__(self):
return self.pi.reference_id
这是我的 view.py
from django.views.generic.edit import CreateView
from pi.models import Items, PiItem, Pi
from django.shortcuts import get_object_or_404
class AddItemView(CreateView):
context_object_name = 'Product_list'
model = Items
template_name = "product.html"
fields = "__all__"
def get_initial(self):
in_pi = get_object_or_404(PiItem)
return {
'in_pi':in_pi
}
这是模板
{% extends 'base.html' %}
{% load crispy_forms_tags %}
{% block pi %}
<form action="" method="post">{% csrf_token %}
{{ form|crispy }}
{{ form.get_form_class() }}
<input type="submit" value="Create" />
</form>
问题最有可能出在这里:
in_pi = get_object_or_404(PiItem)
您需要通过向每个相关模型传递额外的参数(例如外键)来过滤 PiItem 对象,例如:
in_pi = get_object_or_404(PiItem, item_name=something1, pi=something2)