GreenAsJade
GreenAsJade

Reputation: 14685

Why doesn't multiple inheritance work in Django views?

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']     

Upvotes: 0

Views: 1424

Answers (1)

sax
sax

Reputation: 3806

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?

Upvotes: 4

Related Questions