标签: formset

Django - 在BaseInlineFormSet的子类中更改字段的小部件

我有一个表单是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)

任何帮助和输入表示赞赏

django formset

1
推荐指数
1
解决办法
1976
查看次数

在formset中访问特定表单而不对索引进行硬编码

有没有办法从模板中的内联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)

有办法实现这个吗?

django formset inline-formset

1
推荐指数
1
解决办法
953
查看次数

Django:AttributeError:视图对象没有属性“kwargs”

我正在构建一个视图,我正在从 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)

关于正在发生的事情的背景——

  • Post 是一个模型,它是其他模型(产品和变体)继承的InheritanceManager
  • 两种模型产品和变体都有很多细节。
  • 创建 Detail 后,我会将其添加到 Product 对象或 Variation 对象。
  • 为了为 InlineFormSetView 设置模型,我尝试使用 UUID …

django formset inline-formset

1
推荐指数
1
解决办法
1382
查看次数

Django - 用基数为10的int()的Formset无效文字:''

我的表单中有一个内联字段,我试图通过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 …

django admin save formset

0
推荐指数
1
解决办法
3084
查看次数

标签 统计

django ×4

formset ×4

inline-formset ×2

admin ×1

save ×1