我有一个表单是BaseInlineFormSet的子类,对于相关模型中的一个字段,我喜欢更改它的小部件.这是我的表单代码:
class MyForm(forms.models.BaseInlineFormSet):
def __init__(self, *args, **kwargs):
super(MyForm, self).__init__(*args, **kwargs)
self.forms[0].error_css_class = 'error'
self.forms[0].required_css_class = 'required'
class Meta:
model = MyModel
# here I am trying.
# recommend is an IntegerField in MyModel, which -
# I like to make it render in the form as a Radio Select (yes, no)
widgets = {'recommend': {forms.RadioSelect}}
Run Code Online (Sandbox Code Playgroud)
任何帮助和输入表示赞赏
有没有办法从模板中的内联formset访问特定表单,而无需对索引进行硬编码?我知道迭代一个formset的通常方法是做类似的事情:
{% for form in formset %}
{{ form }}
{% endfor %}
Run Code Online (Sandbox Code Playgroud)
但是由于模板上的一些细节(我有多个表单集,应该在表格上并排显示在另一个表格内for),如果我可以通过索引访问每个表单会更好.我可以通过硬编码索引来做到这一点{{ formset.0 }},但是因为我在模板中迭代,理想的是通过forloop.counter获取表单,这样我就可以做类似的事情了
{% for field in fields %}
<tr>
<td>{{ field }}</td>
<td>{{ formset1.[forloop.counter0] }}</td>
<td>{{ formset2.[forloop.counter0] }}</td>
</tr>
{% endfor %}
Run Code Online (Sandbox Code Playgroud)
有办法实现这个吗?
我正在构建一个视图,我正在从 url 传递一个 uuid。但是,当我尝试访问 kwarg 时,出现"AttributeError: view object has no attribute 'kwargs'"错误。
在我的模板中,我传递了一个 UUID:
create/97261b96-23b8-4915-8da3-a90b7a0bdc8e/
Run Code Online (Sandbox Code Playgroud)
网址:
re_path(
r"^create/(?P<uuid>[-\w]+)/$",
views.DetailCreateView.as_view(),
name="detail_create"),
Run Code Online (Sandbox Code Playgroud)
风景:
class DetailCreateView(SetHeadlineMixin, LoginRequiredMixin, InlineFormSetView):
inline_model = Detail
headline = "Create a Detail"
form_class = DetailForm
success_message = "Detail Added"
template_name = "details/detail_create.html"
def get_object(self, **kwargs):
return Post.objects.get_subclass(uuid=self.kwargs.get('uuid'))
def __init__(self, *args, **kwargs):
super(DetailCreateView, self).__init__(*args, **kwargs)
self.object = self.get_object()
self.model = self.object.__class__()
Run Code Online (Sandbox Code Playgroud)
关于正在发生的事情的背景——
我的表单中有一个内联字段,我试图通过save_formset()函数访问它
这是我的代码:
def save_formset(self, request, form, formset, change):
periods = formset.save(commit=False)
match = periods[0].match
match_utils = MatchUtils()
teamstat_utils = TeamStatUtils()
is_update = match.reported
match_type = match.tournament.type
# Find positions
first_place = match_utils.findPositions(periods,match.blue_team,match.grey_team,match.black_team,1)
second_place = match_utils.findPositions(periods,match.blue_team,match.grey_team,match.black_team,2)
third_place = match_utils.findPositions(periods,match.blue_team,match.grey_team,match.black_team,3)
# Update Match
match.first_place = first_place
match.second_place = second_place
match.third_place = third_place
match.blue_periods_won = match_utils.findTeamPeriodsWon(periods,'blue_team')
match.grey_periods_won = match_utils.findTeamPeriodsWon(periods,'grey_team')
match.black_periods_won = match_utils.findTeamPeriodsWon(periods,'black_team')
match.reported = True
# Update team stats
teamstat_utils.updateTeamStats(match.blue_team,match_utils.findPositionForTeam(match.blue_team,first_place,second_place,third_place),
match.blue_periods_won,match.grey_periods_won+match.black_periods_won,is_update,match_type) #Blueteam
teamstat_utils.updateTeamStats(match.grey_team,match_utils.findPositionForTeam(match.grey_team,first_place,second_place,third_place),
match.grey_periods_won,match.blue_periods_won+match.black_periods_won,is_update,match_type) #Greyteam
teamstat_utils.updateTeamStats(match.black_team,match_utils.findPositionForTeam(match.black_team,first_place,second_place,third_place),
match.black_periods_won,match.blue_periods_won+match.grey_periods_won,is_update,match_type) #Blackteam
pdb.set_trace()
match.save()
formset.save()
Run Code Online (Sandbox Code Playgroud)
但我一直得到:基数为10的int()的无效文字:''
我不知道为什么,我尝试了formset.is_valid()并返回True …