有几个 ForeignKeysModelForm
我想在表单向导中使用modelformset_factory
(不是 100% 确定表单集)我想知道如何限制下拉字段的选择,因为我需要动态地执行它。我想通过编写自己的 modelformset 工厂来尝试它,但在 stackoverflow 上我读到了其他方法,但不幸的是我不理解它们。
这就是我走了多远:
模型.py
#...
class Attendee(models.Model):
"""Event specific attendee details."""
# event is set by URL.
event = models.ForeignKey(Event)
attendee = models.ForeignKey(Person) # Contact details, should be limited to user
accommodation = models.ForeignKey(Accommodation, blank=True) # *
workshop = models.ForeignKey(Workshop, blank=True) # *
volunteer = models.ForeignKey(Volunteer, blank=True) # *
# *= should be limited to event but I think I will be able to handle that.
#...
class AttendeeForm(forms.ModelForm):
class Meta:
model = Attendee
def __init__(self, *args, **kwargs):
# def __init__(self, user, *args, **kwargs): I tried this but I didn't get
# it to work.
super(AttendeeForm, self).__init__(*args, **kwargs)
# self.fields['attendee'].queryset = Person.objects.filter(owner=user)
self.fields['workshop'].required = True
#...
# This is from https://stackoverflow.com/a/4858044/2704544 but I don't understand it. ##
def form_setup(**kwargs):
def make_form(data, prefix=None, initial=None):
form = (data, prefix, initial)
for k, v in kwargs.items():
if k == 'some_list':
form.fields['some_list'].choices = v # What does that mean?
...
return form
return make_form
#######################################################################################
(链接到来源)
视图.py
# This is from https://stackoverflow.com/a/623198/2704544 but I don't understand it. ###
class Callback(object):
def __init__(self, field_name, aff):
self._field_name = field_name
self._aff = aff # What is this aff?
def cb(self, field, **kwargs):
nf = field.formfield(**kwargs)
if field.name == self._field_name: # this is 'options' field
nf.queryset = ServiceOption.objects.filter(affiliate=self._aff)
return nf
#######################################################################################
reg_wiz_forms = (
('attendees', modelformset_factory(Attendee, form=AttendeeForm, exclude='event',
# formfield_callback=Callback('option', affiliate).cb Just copied. Why can you call
# cb without arguments?
)),)
class RegWizard(SessionWizardView):
def get_form_instance(self, step):
instance = None
if step == '0':
instance = Attendees.objects.filter(owner__owner=self.request.user)
#...
(链接到来源)
网址.py
#...
url(r'^register/$', views.RegWizard.as_view(views.reg_wiz_forms), name='register'),
#...
我还阅读了关于curry
和其他内容,并尝试覆盖get_initkwargs
和其他方法,WizardView
但我找不到更多关于该主题的文档或提示。也许有人可以帮助我。
更新
它部分适用于curry
现在。部分原因是它不适用于管理功能:
视图.py
def manage_wizard(request, event):
AttendeeFormSet = modelformset_factory(Attendee, form=AttendeeForm, exclude='event')
AttendeeFormSet.form = staticmethod(curry(AttendeeForm, user=request.user))
wiz = RegWizard
wiz.event = event
return wiz.as_view([('attendees', AttendeeFormSet)])
我得到一个 AttributeError: "'function' object has no attribute 'base_fields'" 这听起来像这里的同一个问题。
但是当我覆盖WizardView
'sget_form_list
并直接在 url.py 中调用它时,它可以工作:
reg_wiz_forms = ('attendees', modelformset_factory(Attendee, form=AttendeeForm, exclude='event'))
class RegWizard(SessionWizardView):
def get_form_list(self):
self.form_list['attendees'].form = staticmethod(curry(AttendeeForm, user=self.request.user))
return super(RegWizard, self).get_form_list()
现在我想知道是否有不覆盖该方法的解决方案。