0

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']     
Community
  • 1
  • 1
GreenAsJade
  • 14,459
  • 11
  • 63
  • 98
  • I just discovered that it is the model specifier, not the field list, that is the problem. Inheriting the field list works fine, but the view appears to need the model specifier declared directly in it. Is that wierd? – GreenAsJade Dec 14 '14 at 21:40

1 Answers1

4

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?

Community
  • 1
  • 1
sax
  • 3,708
  • 19
  • 22