Django与来自多个模型的数据一起形成(Django forms with data from multiple models)

编程入门 行业动态 更新时间:2024-10-26 10:33:42
Django与来自多个模型的数据一起形成(Django forms with data from multiple models)

最简单的解释方法就是说出我要做的事情。

我有一个访问模型和一个主题模型。 访问是主题的外键,因此我可以将主题作为我的表单中的选择字段(这是一个基于访问模型构建的模型表单)。

我想要的其他数据虽然与主题有关,但我想知道最好的访问方式是什么。

我的模型,形式和我最终想要的是下面的。

楷模

class Subject(models.Model): def __unicode__(self): return self.subject_name subject_name=models.CharField(max_length=60,null=True, blank=True) subject_number=models.CharField(max_length=10,null=True, blank=True) birthdate=models.DateField("Date of Birth",null=True, blank=True) cohort=models.ForeignKey("Cohort",null=True, blank=True) class Visit(models.Model): def __unicode__(self): return self.id subject=models.ForeignKey("Subject") ageband=models.ForeignKey("AgeBand",null=True, blank=True) visit_date=models.DateField("Visit Date",null=True, blank=True)

形成

class TestForm(ModelForm): class Meta: model = Visit widgets = { 'visit_date': DateInput(attrs={'class':'datepicker'}), }

简单描述我想要的形式

主题名称_ (有这个)

主题编号_ _(有这个)

主题出生日_ _(来自Subject.birthdate,没有这个)

访问日期_ (有这个)

Simplest way to explain would be to just say what I'm trying to do.

I have a Visit model and a Subject model. The Visit is foreign-keyed to the Subject, so I can get the Subjects as a choice field in my form (which is a Model Form built off of the Visit model).

There is other data I want though that is related to the subject, and I was wondering what the best way to access that is.

My models, form and what I ultimately want are below.

Models

class Subject(models.Model): def __unicode__(self): return self.subject_name subject_name=models.CharField(max_length=60,null=True, blank=True) subject_number=models.CharField(max_length=10,null=True, blank=True) birthdate=models.DateField("Date of Birth",null=True, blank=True) cohort=models.ForeignKey("Cohort",null=True, blank=True) class Visit(models.Model): def __unicode__(self): return self.id subject=models.ForeignKey("Subject") ageband=models.ForeignKey("AgeBand",null=True, blank=True) visit_date=models.DateField("Visit Date",null=True, blank=True)

Form

class TestForm(ModelForm): class Meta: model = Visit widgets = { 'visit_date': DateInput(attrs={'class':'datepicker'}), }

Simple Depiction of what I want in form

Subject Name _ (have this)

Subject Number __ (have this)

Subject Birthdate __ (from Subject.birthdate, don't have this)

Visit Date _ (have this)

最满意答案

ModelForm仅表示来自一个模型的字段。 如果要使用这两个模型,则需要使用InlineFormSet ,它允许您组合两个ModelForm 。 从文档 :

内联表单集是模型表单集之上的一个小抽象层。 这些简化了通过外键处理相关对象的情况。

这是一个类似的答案 ,描述了如何创建InlineFormSet 。

ModelForm only represents fields from one model. If you want to use both models, you'll need to use an InlineFormSet, which allows you to combine two ModelForms. From the documentation:

Inline formsets is a small abstraction layer on top of model formsets. These simplify the case of working with related objects via a foreign key.

Here is a similar answer that describes how to create an InlineFormSet.

更多推荐

本文发布于:2023-07-29 05:10:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1312627.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   模型   数据   Django   models

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!