現在、タイプ のデータベースに一連のオブジェクトを設定していますCellType
。
私が必要としているのはinline formsetです。これにより、モデル間の外部キー関係の操作が明らかに簡素化されます。
私が欲しいのは、データベース内の CellType オブジェクトのそれぞれにリンクされた、CellCount インスタンスごとに 1 つのフォームで構成されるフォームセットです。
cellcount_formset = inlineformset_factory(CellType,
CellCount,
form=CellCountForm,
can_delete=False)
少し正しい方向に進んでいるようですが、実際に私が望むのは、CellCountForm のセル フィールドに正しい CellType オブジェクトを入力して、すべてをきちんとパッケージ化して保存できるようにすることです。これは、私が思っていたよりも少し複雑であることが証明されています!
models.py
class CellType(models.Model):
readable_name = models.CharField(max_length=50)
machine_name = models.CharField(max_length=50)
comment = models.TextField(blank=True)
class CellCount(models.Model):
cell_count_instance = models.ForeignKey(CellCountInstance)
cell = models.ForeignKey(CellType)
normal_count = models.IntegerField()
abnormal_count = models.IntegerField()
comment = models.TextField(blank=True)
フォーム.py
class CellCountForm(ModelForm):
auto_id = False
class Meta:
model = CellCount
widgets = {
'cell': HiddenInput(),
'normal_count': HiddenInput,
'abnormal_count': HiddenInput}
exclude = ('cell_count_instance', 'comment',)
ウィジェットは、舞台裏で JQuery ベースの電卓によって設定されるため、非表示になっています。
理想的には、私が目指している疑似コード ロジックは次のとおりです。
formset containing:
(x) CellCount forms,
the cell field of which is populated with the CellType object
(x) = number of CellTypes in the database