我的模式。 py:
class Company(models.Model):
company_name = models.CharField(max_length=255)
class TimePeriod(models.Model):
period = models.CharField(max_length=4)
class Unit(models.Model):
unit_name = models.CharField(max_length=50)
unit_type = models.CharField(max_length=50)
class DataElement(models.Model):
data_element_name = models.CharField(max_length=50)
class Source(models.Model):
source_name = models.CharField(max_length=50)
class CompanyData(models.Model):
company = models.ForeignKey(Company)
time_period = models.ForeignKey(TimePeriod)
data_element = models.ForeignKey(DataElement)
unit = models.ForeignKey(Unit)
source = models.ForeignKey(Source)
value = models.DecimalField(max_digits=15, decimal_places=2)
我的表格类似于:
--————
- data element1: --value--, --unit--, --source--
- data element2: --value--, --unit--, --source--
- ...
- data elementN: --value--, --unit--, --source--
我的发言。 现在:
class CompanyTimeForm(ModelForm):
class Meta:
model = CompanyData
fields = ( company , time_period )
class CompanyDataForm(ModelForm):
# data_element = forms.IntegerField(widget=forms.HiddenInput())
# def __init__(self, extra):
# ... init data_element ...
class Meta:
model = CompanyData
fields = ( value , unit , source )
使用:
top_form = CompanyChainTimeForm(request.POST or None)
CompanyDataFormSet = formset_factory(CompanyDataForm, extra=X)
formset = CompanyDataFormSet()
Is it possible to initialize empty formset, then fetch DataElement.objects.all() and make something similar to formset.add(CompanyDataForm(...DataElement.pk...))
或者我的案件的最佳选择是什么?