我想知道是否有一种方法可以在一个视图中包含多个不同的表单,并按下一个提交按钮来存储信息。 我有以下表单,第一个是我应该首先创建的通用表单:
class GeneralForm(forms.ModelForm):
name = forms.CharField(max_length=50)
TYPE_CHOICES = (
("C","C"),
("E","E")
)
STATUS_CHOICES = (
("A", "A"),
("F", "F"),
)
type=forms.ChoiceField(choices=STATUS_CHOICES)
number=forms.CharField(max_length=50)
TURN_CHOICES = (
("1", "1"),
("2", "2"),
("3", "3")
)
turn = forms.ChoiceField(choices=TURN_CHOICES)
class Meta:
model = models.General
fields=["name","type","number","turn"]
第二个需要第一个的实例,第三个也需要:
class TypeOneForm(forms.ModelForm):
num_chairs=forms.IntegerField()
num_instalations = forms.IntegerField()
total = forms.IntegerField()
num_programs = forms.IntegerField()
class Meta:
model = models.TypeOne
fields=["num_chairs","num_instalations","total","billetes_cien"]
class TypeTwoForm(forms.ModelForm):
num_available_seats=forms.IntegerField()
num_available_instalations = forms.IntegerField()
total = forms.IntegerField()
num_new_programs = forms.IntegerField()
class Meta:
model = models.TypeTwo
fields=["num_available_seats", "num_available_instalations","total","num_programs" ]
这些是我的模型:
from django.db import models
from django.utils import timezone
from django.contrib.auth.models import User
from django.urls import reverse
# Create your models here.
class General(models.Model):
author = models.ForeignKey(User, on_delete=models.CASCADE, default=1)
name = models.CharField(max_length=100)
TYPE_CHOICES = {
"C": "C",
"E": "E",
}
type = models.CharField(max_length=10, choices=TYPE_CHOICES)
STATUS_CHOICES = {
"A": "A",
"F": "F",
}
status = models.CharField(max_length=10, choices=STATUS_CHOICES)
number = models.CharField(max_length=100)
TURN_CHOICES = {
"1": "1",
"2": "2",
"3": "3",
}
turn = models.CharField(max_length=10, choices=TURN_CHOICES)
def __str__(self):
return self.name
class One(models.Model):
one = models.OneToOneField(General, on_delete=models.CASCADE, primary_key=True)
num_chairs = models.IntegerField(default=0)
num_installations = models.IntegerField(default=0)
total = models.IntegerField(default=0)
num_programs = models.IntegerField(default=0)
class Two(models.Model):
two = models.OneToOneField(General, on_delete=models.CASCADE, primary_key=True)
num_available_seats = models.IntegerField(default=0)
num_available_installations = models.IntegerField(default=0)
total = models.IntegerField(default=0)
num_new_programs = models.IntegerField(default=0)
这些是我的观点,但我也不确定如何针对这个特定情况编写创建视图和更新视图。
from django.shortcuts import render, get_object_or_404
from django.contrib.auth.mixins import LoginRequiredMixin, UserPassesTestMixin
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User
from django.views.generic import (
ListView,
DetailView,
CreateView,
UpdateView,
)
from .models import General, One,Two
# Create your views here.
class GeneralListView(ListView):
model = General
template_name = "testing/user_testing.html"
context_object_name = 'generals'
class GeneralDetailView(DetailView):
model = General
class GeneralCreateView(LoginRequiredMixin, CreateView):
model = General
fields = '__all__'
def form_valid(self, form):
form.instance.author = self.request.user
return super().form_valid(form)
class GeneralUpdateView(LoginRequiredMixin, UpdateView):
pass
我正在阅读我可以使用 FormSets,但我不确定是否可以使用不同的表单而不是同一表单的多个实例。
您在这里寻找的工具是表单类的前缀参数。您需要重写一些视图方法,例如 get_form 和可能的 get_form_class ,因为基于类的视图默认情况下不会采用多种表单类型。您可能还需要重写视图上的 post 方法,以将 post 请求中的数据加载到表单中。
前缀参数意味着发送到 django 应用程序的表单数据将被“命名空间”,因此表单类只会加载请求中所需的发布数据。
根据您呈现表单的方式,您可能希望确保前缀也呈现在 HTML 输入的 name 属性中。