Django - CreateView get_context_data TypeError

时间:2015-04-08 04:51:27

标签: python django django-views

我的app / views.py上有一个mixin:

class RefereeViewMixin(object):
    @method_decorator(login_required)
    def dispatch(self, *args, **kwargs):
        if not self.request.user.is_superuser and \
           not self.request.user.has_group("referee"):
            raise PermissionDenied
        return super(RefereeViewMixin, self).dispatch(*args, **kwargs)

还有一个来自我的自定义mixin和CreateView的视图:

class RefereeResultCreateView(RefereeViewMixin, CreateView):
    category = None
    template_name = "referee/result_create.html"
    fields = ["minutes", "seconds", "miliseconds", "disqualification"]

    def get_success_url(self):
        return reverse("order_list", args=[self.category])

    def get_context_data(self, **kwargs):
        context = super(RefereeResultCreateView, self).get_context_data(
            **kwargs)
        context["project"] = Project.objects.get(pk=self.kwargs.get("pk"))
        return context

    def form_valid(self, form):
        result = form.save(commit=False)
        result.project = Project.objects.get(pk=self.kwargs.get("pk"))
        result.save()

        messages.success(self.request, _(
            "Result entry for {} project created.".format(result.name)))
        return super(RefereeResultCreateView, self).form_valid(form)

最后,我的最终视图来自RefereeResultCreateView:

class RefereeFireFighterResultCreateView(RefereeResultCreateView):
    model = FireFighterResult
    category = "fire_fighter"
    fields = RefereeResultCreateView.fields + [
        "extinguish_success", "extinguish_failure", "wall_hit"]

模式就是这样:

    url(r'^fire_fighter/(?P<pk>\d+)/create/$',
        RefereeFireFighterResultCreateView.as_view(),
        name="referee_fire_fighter_result_create")

然而,当我去/ app / fire_fighter / some_id / create /我得到TypeError。它告诉我 super(type,obj):obj必须是类型的实例或子类型。当我调试它时,我发现 RefereeResultCreateView get_context_data 方法会导致该错误。如果我不重写该方法,我没有看到任何错误。当我在RefereeFireFighterResultCreateView类中覆盖它时,我没有看到任何错误。

我可以克服这个问题。但是,我认为这不是优雅的方式。实际上,它非常脏。另外,我很好奇这个原因。为什么它不符合我的预期?什么是优雅的解决方案?

已更新

这是追溯:

https://gist.github.com/ilkerkesen/e65fefa66890758bcdcd

0 个答案:

没有答案