0

我希望能够在餐桌上举办派对。首先我需要显示表单,所以我创建了一个模型表单,其中包含一个名为 open_tables 的额外字段。这必须是每个标记为 AVAILABLE 且具有容量的表。问题是我不知道如何从查询集中引用 number_in_party 字段。我已经绑定了 self.base_fields,但 self 不起作用。我试过 SeatPartyForm.model.number_in_party,但还是不行。这发生在 get 上,此时 number_in_party 已填充。有没有办法做这个查询?

class SeatPartyForm( ModelForm):

open_tables = forms.ModelChoiceField(queryset=Table.objects.filter(status__exact=Table.AVAILABLE).exclude(max_capacity__lt =  model.base_fields['number_in_party']))
class Meta:
    model = Party
    fields = ('name', 'number_in_party')`
4

1 回答 1

0

You don't know the actual number_in_party before user fills in one. The server-side Django form cannot do the limitation automatically for you.

Thus you need to either change open_tables after number_in_party is available, via javascript; or split the form to two parts, in the first part user fills in number_in_party, in the second part, in server side you could filter open_tables according to the ready value of number_in_party.

于 2012-06-09T07:03:35.013 回答