问题描述
我可以使用Django 创建视图以创建将数据添加到多个表的表单?我创建了一个名为 UserMeta 的模型来存储我的用户的一些其他信息.
Can I use Django CreateViews to make a form that add data to multiple tables?I've created a model called UserMeta to store some additional informations of my users.
我想用CreateViews创建一个视图,该视图显示用于创建新用户的字段( Django用户模型)和一些基于我的UserMeta模型的额外字段:
I want to create a view with CreateViews, that display fields for creating a new user (Django User Model) and some extra fields based on my UserMeta model:
class UserMeta(models.Model):
country_code = models.CharField(max_length=30, verbose_name="Nome",
help_text="Insert the name of the country")
我需要这样的输出模式:
I need an output schema like this:
|-输入用户名
|-输入user.username
|-- input user.username
|-输入用户....
|-- input user....
|-输入usermeta.country_code
|-- input usermeta.country_code
推荐答案
您可以使用>> django-extra-views ,这是其他CBV的集合.
You can use django-extra-views
, a collection of additional CBVs, for this purpose.
from extra_views import CreateWithInlinesView InlineFormSet
class UserMetaInline(InlineFormSet):
model = UserMeta
class UpdateOrderView(CreateWithInlinesView):
model = User
inlines = [UserMetaInline,]
虽然这些内联通常用于外键关系,但它们也一对一地起作用.
While these inlines are generally used for foreign key relations, they work for one-to-one as well.
这篇关于Django-具有多个模型的CreateView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!