我有一个用户可以定义产品的设置(例如' MacBook Pro')。每个Product
获得"实例化"创建一个获取序列号的物理项(ProductInstance
)。到目前为止非常简单。
我希望用户能够定义其他类型的唯一标识符,例如MAC地址,VIN,社会安全号码等。我称之为UIDType
。每个产品可以包含零个或多个UIDType
。这些需要通过分配名称/描述并链接到Product
和UIDType
(UIDProductInfo
)来以某种方式区分。要实际分配值,我需要为特定UIDProductInfo
的{{1}}分配值 - 这是使用ProductInstance
模型完成的。
当用户实例化产品(创建UID
)时,他们应该被迫为ProductInstance
UID
的每个UIDProductInfo
创建一个Product
个&# 39;重新实例化。我想在一个页面上完成所有操作。这是一个问题:使用CreateView
,我该怎么做?
我认为我需要ModelForm
ProductInstance
和一个处理UID的formset。 UID
formset中的表单需要创建一个UID
,该UIDProductInfo
绑定到与Product
关联的每个ProductInstance
对象以及新创建的initial
1}}。不幸的是,我不知道该怎么做。我在CreateView
get_context_data
方法中创建表单集时尝试使用CreateView.get_context_data
参数,还有一些其他更常用的方法,但我无法获得formset正确创建。
感谢您的帮助。模型和我对# a product, i.e. Toyota Corolla
class Product(models.Model):
name = models.CharField(max_length = 255)
identifiers = models.ManyToManyField('UIDType', related_name = 'products', through = 'UIDProductInfo')
# a physical item, an "instantiation" of a Product. Gets a serial number
class ProductInstance(models.Model):
serial_number = models.CharField(max_length = 255)
product = models.ForeignKey(Product, related_name = 'instances')
# a type of unique identifier, like a MAC address
class UIDType(models.Model):
name = models.CharField(max_length = 255, unique = True)
# a description of a UIDType as it applies to a particular Product.
# for example, a computer might get two MAC addresses, 'Host MAC 1' and 'Host MAC 2'
class UIDProductInfo(models.Model):
name = models.CharField(max_length = 255)
type = models.ForeignKey(UIDType, related_name = 'info')
product = models.ForeignKey(Product, related_name = 'product_identifiers')
# a unique identifier for a ProductInstance
class UID(models.Model):
value = models.CharField(max_length = 255)
info = models.ForeignKey(UIDProductInfo, related_name = 'values')
product_instance = models.ForeignKey(ProductInstance, related_name = 'identifiers')
方法的悲伤尝试在
模型:
def get_context_data(self, **kwargs):
self.object = None
context = super(ProductInstanceCreate, self).get_context_data(**kwargs)
product = get_object_or_404(Product, id = self.kwargs.get('product_pk'))
uid_info = [ ]
pi = ProductInstance(product = product)
for info in pi.product.product_identifiers.all():
uid = UID(productInstance = pi, id_type = info)
uid_info.append({'info':uid})
UIDFormset = inlineformset_factory(ProductInstance, UID,
fields = ('identifier', 'info',), can_delete = False,
widgets = { 'info' : HiddenInput() },
labels = {'identifier' : ''}, extra = 3)
uid_formset = UIDFormset(
instance = pi,
initial = uid_info
)
context['identifiers_formset'] = uid_formset
return context
get_context_data:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title></title>
<script>
window.onload = function() {
var innerDiv = document.getElementById("innerDiv");
var newDiv = document.createElement("div");
for (var i = 0; i < innerDiv.childNodes.length; i++) {
var anchor =
newDiv.appendChild(document.createElement("a"));
anchor.setAttribute("href", "http://www.bing.ca");
anchor.text = innerDiv.childNodes[i].textContent;
newDiv.appendChild(document.createElement("br"));
}
innerDiv.replaceNode(newDiv);
}
</script>
</head>
<body>
<div id="outerDiv">
<p class='mainPara'>Main Paragraph</p>
<ul>
<li>First List Item</li>
<li>Second List Item</li>
<li>Third List Item</li>
<li>Fourth List Item</li>
</ul>
<div id="innerDiv">
<p class='subPara' id='P1'>Paragraph 1</p>
<p class='subPara' id='P2'>Paragraph 2</p>
<p class='subPara' id='P3'>Paragraph 3</p>
<p class='subPara' id='P4'>Paragraph 4</p>
</div>
<table>
</table>
<input type="text"/><input type="submit" value="Submit!">
</body>
</html>
答案 0 :(得分:0)
我使用此处描述的技术解决了这个问题,而不是使用formset:
https://collingrady.wordpress.com/2008/02/18/editing-multiple-objects-in-django-with-newforms/