在Django中使用CreateView创建复杂的多模型对象
我有一个设置,用户可以在其中定义产品(如“MacBook Pro”)。每个在Django中使用CreateView创建复杂的多模型对象,django,django-forms,formset,inline-formset,Django,Django Forms,Formset,Inline Formset,我有一个设置,用户可以在其中定义产品(如“MacBook Pro”)。每个产品都“实例化”以创建一个物理项(ProductInstance),该物理项获取一个序列号。到目前为止相当简单 我希望用户能够定义其他类型的唯一标识符,如MAC地址、VIN、社会保险号等。我称之为UIDType。每个产品可以有零个或多个UIDType。需要通过指定名称/描述并链接到产品和UIDType(UIDProductInfo)以某种方式区分它们。要实际分配值,我需要为特定的ProductInstance的UIDPro
产品
都“实例化”以创建一个物理项(ProductInstance
),该物理项获取一个序列号。到目前为止相当简单
我希望用户能够定义其他类型的唯一标识符,如MAC地址、VIN、社会保险号等。我称之为UIDType
。每个产品可以有零个或多个UIDType
。需要通过指定名称/描述并链接到产品
和UIDType
(UIDProductInfo
)以某种方式区分它们。要实际分配值,我需要为特定的ProductInstance
的UIDProductInfo
分配一个值——这是通过UID
模型完成的
当用户实例化一个产品(创建一个ProductInstance
)时,应强制他们为正在实例化的产品的每个UIDProductInfo
创建一个且仅创建一个UID
。我想在一页纸上完成这一切。这里有一个问题:使用CreateView
,我如何做到这一点
我想我需要一个用于ProductInstance
的ModelForm
和一个用于处理UID的表单集。UID
表单集中的表单需要创建一个UID
,它绑定到与Product
关联的每个UIDProductInfo
对象以及新创建的ProductInstance
。不幸的是,我不知道怎么做。在CreateView
的get\u context\u data
方法中创建表单集时,我尝试使用initial
参数,以及其他一些更麻烦的方法,但我就是无法正确创建表单集
谢谢你的帮助。下面是模型和我在CreateView.get\u context\u data
方法中的悲伤尝试
型号:
# 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
我使用这里描述的技术解决了这个问题,而不是使用表单集: