1

有几个 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()

现在我想知道是否有不覆盖该方法的解决方案。

4

1 回答 1

5

您只希望您的模型上的参加者属性的外键查询集成为您的模型窗体上的过滤查询集。你在这里是正确的:

self.fields['attendee'].queryset = Person.objects.filter(owner=user)

这是假设 Person 类上存在属性“所有者”。

但是,这不起作用,因为您的用户参数在哪里或是什么?正如您所提到的,一种解决方案是使用表单init方法来包含正确的用户对象:

    form = staticmethod(curry(AttendeeForm, user=<the-user-obj>))

现在,您从init方法中的 kwargs 中弹出您的用户 arg :

user = kwargs.pop('user')

现在您过滤的查询集将只显示您过滤的人。

def __init__(self, user, *args, **kwargs): I tried this but I didn't get 
it to work.

由于多种原因,上面的行不起作用,主要是它永远不会从任何地方调用,你正在那里创建一个新函数,而不是覆盖现有的函数,这就是我们正在做的覆盖init方法的事情。

只是一些可能对设计有用的建议——你在这里有很多线程,都提供了许多不同的想法,使事情复杂化了很多。尝试将您的问题过滤到基本概念。在这里,它是数据问题的呈现,所以考虑从表单开始,这就是它的用途。:-)

于 2013-09-02T18:32:58.667 回答