我有以下型号,
### models.py
class Foo(models.Model):
name = models.CharField(max_length=200)
class Bar(models.Model):
foo = models.ForeignKey(Foo)
baz = models.ManyToManyField(Baz, through='Between')
class Baz(models.Model):
name = models.CharField(max_length=200)
class Between(models.Model):
foo = models.ForeignKey(Foo)
bar = models.ForeignKey(Bar)
CHOICES = (
('A', 'A'),
('B', 'B'),
('C', 'C'),
)
value = models.CharField(max_length=1, choices=CHOICES)
我有以下表格,
### forms.py
class FooForm(forms.ModelForm):
class Meta:
model = Foo
class BarForm(forms.ModelForm):
baz = forms.ModelMultipleChoiceField(widget=forms.CheckboxSelectMultiple(),
queryset=Baz.objects.all())
class Meta:
model = Bar
exclude = ('foo',)
BarFormSet = inlineformset_factory(Foo, Bar, form=BarForm, can_delete=False)
现在,这很有效,因为我可以渲染一个Foo
,并为Bar
获得了许多内联表单。此呈现是内联BarForm
将Baz
的所有选项呈现为复选框。
我希望将Baz
的每条记录呈现为一组代表value
可能选择的单选按钮以及“N / A”选项 - - 如果选择A,B或C,则隐含与Baz
的关系。但是默认情况下,通过完全重新实现RadioSelect或实现一个全新的小部件似乎没有一个很好的方法,但我想遵循阻力最小的路径。
希望我能说清楚。
答案 0 :(得分:0)
我的解决方案是创建允许子表单的SuperForm
。对于每个人来说,整体解决方案并不完全通用,但我在这里尽力采取我正在使用的内容并使其更加通用。
以下是models.py
:
from django.db import models
class Enemy(models.Model):
name = models.CharField(max_length=200)
def __unicode__(self):
return self.name
class Hero(models.Model):
name = models.CharField(max_length=200)
enemy = models.ManyToManyField(Enemy, through='Relationship', blank=False, null=False)
def __unicode__(self):
return self.name
class Relationship(models.Model):
hero = models.ForeignKey(Hero)
enemy = models.ForeignKey(Enemy)
ENEMY_TYPE_CHOICES = (
('G', 'Good'),
('B', 'Bad'),
('U', 'Ugly'),
)
enemy_type = models.CharField(max_length=1, choices=ENEMY_TYPE_CHOICES)
def __unicode__(self):
return u"{0} {1} {2}".format(self.hero, self.strength, self.relationship)
以下是forms.py
:
from models import *
from django import forms
from django.forms.formsets import formset_factory, BaseFormSet
from django.forms.models import inlineformset_factory, BaseModelFormSet
from django.utils.safestring import mark_safe
class SuperForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
self.formsets = []
super(SuperForm, self).__init__(*args, **kwargs)
@property
def is_bound(self):
return self._is_bound or \
any([subformset.is_bound for subformset in self.formsets])
@is_bound.setter
def is_bound(self, value):
self._is_bound = value
def has_changed(self):
return bool(self.changed_data) or \
any([subformset.has_changed() for subformset in self.formsets])
def update_empty_permitted(self, subformset):
empty_permitted = not self.has_changed()
for subformset in self.formsets:
for form in subformset:
form.empty_permitted = empty_permitted
def is_valid(self):
subforms_valid = True
for subformset in self.formsets:
self.update_empty_permitted(subformset)
sfserrors = [err for err in subformset.errors if err]
if bool(sfserrors):
subforms_valid = False
return subforms_valid and super(SuperForm, self).is_valid()
class RelationshipForm(forms.ModelForm):
enemy = forms.ModelChoiceField(queryset=Enemy.objects.all(),
widget=forms.HiddenInput(),
required=True)
enemy_type = forms.ChoiceField(label="", widget=forms.RadioSelect,
choices=Relationship.ENEMY_TYPE_CHOICES,
required=True)
def name(self):
pk = self['enemy'].value()
return self.fields['enemy'].queryset.get(pk=pk)
class Meta:
model = Relationship
exclude = ('hero', 'enemy_type',)
RelationshipFormSet = formset_factory(RelationshipForm, extra=0, can_delete=False)
class HeroForm(SuperForm):
def __init__(self, *args, **kwargs):
super(HeroForm, self).__init__(*args, **kwargs)
initial=[dict(enemy=enemy.pk) for enemy in Enemy.objects.all()]
self.relationship_formset = RelationshipFormSet(initial=initial, prefix=self.prefix, data=kwargs.get('data'))
self.formsets.append(self.relationship_formset)
class Meta:
model = Hero
exclude = ('enemy',)
HeroFormSet = formset_factory(HeroForm, extra=1, can_delete=False)
以下是views.py
:
from django.views.generic import TemplateView
from django.views.generic.edit import FormMixin
from forms import *
class HeroView(FormMixin, TemplateView):
template_name = "formfun/hero.html"
form_class = HeroFormSet
def get_context_data(self, **kwargs):
context = super(HeroView, self).get_context_data(**kwargs)
context['formset'] = HeroFormSet()
return context
def form_valid(self, form):
return render_to_response({'form':form, 'valid': True})
def form_invalid(self, form):
return render_to_response({'form':form, 'valid': False})
这是我渲染它的模板:
{% extends "base.html" %}
{% block title %}form fun{% endblock %}
{% block content %}
<form>
{% for form in formset %}
{{ form.as_p }}
{% for subform in form.relationship_formset %}
<label for="id_{{ subform.html_name }}_0" class="label50">{{ subform.name }}</label>
{{ subform.as_p }}
{% endfor %}
{% endfor %}
</form>
{% endblock content %}