Why doesn't multiple inheritance work in Django views?

浪子不回头ぞ 提交于 2019-12-25 01:28:49

问题


I am trying to avoid repeating the list of fields and model specifier in both a form and a (class based) view.

This answer suggested defining a "meta class" that has the field list in it, and inheriting that class in both the form and the view.

It works fine for the form, but the following code inheriting the list and target model into the view results in this error:

TemplateResponseMixin requires either a definition of 'template_name' or an implementation of 'get_template_names()'

I'm at a loss to see how this change causes that error.


forms.py:

class ScenarioFormInfo:
    model = Scenario
    fields = ['scenario_name', 'description', 'game_type', 
              'scenario_size', 'weather', 'battle_type', 'attacker',
              'suitable_for_axis_vs_AI', 'suitable_for_allies_vs_AI', 
              'playtested_H2H', 'suitable_for_H2H',
              'scenario_file', 'preview']     


class ScenarioForm(forms.ModelForm):
    Meta = ScenarioFormInfo

views.py:

    class ScenarioUpload(generic.CreateView, forms.ScenarioFormInfo):
        form_class = ScenarioForm
#        model = Scenario
#        fields = ['scenario_name', 'description', 'game_type', 
#                  'scenario_size', 'weather', 'battle_type', 'attacker',
#                  'suitable_for_axis_vs_AI', 'suitable_for_allies_vs_AI', 
#                  'suitable_for_H2H', 'playtested_H2H',
#                  'scenario_file', 'preview']     

回答1:


do not mix new style object and old style object, change your class definitions as

class ScenarioFormInfo(object)

put your Mixin as first

class ScenarioUpload(forms.ScenarioFormInfo, generic.CreateView):

read this question about How does Python's super() work with multiple inheritance?



来源:https://stackoverflow.com/questions/27474351/why-doesnt-multiple-inheritance-work-in-django-views

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!