#1
by
crispi
(edited )
Hi everybody, I am using dynamic form fields and have figured out that they are behind an error that I get: `Request Method: GET` `Request URL: https://blah.herokuapp.com/p/h8lpklyu/the_button/Survey/17/` `Django Version: 2.2.12` `Exception Type: ImproperlyConfigured` `Exception Value: Calling modelform_factory without defining 'fields' or 'exclude' explicitly is prohibited. ` `Exception Location: /app/.heroku/python/lib/python3.8/site-packages/django/forms/models.py in modelform_factory, line 545` `Python Executable: /app/.heroku/python/bin/python` `Python Version: 3.8.10` `Python Path: ['/app/.heroku/python/bin', '/app', '/app/.heroku/python/lib/python38.zip', '/app/.heroku/python/lib/python3.8', '/app/.heroku/python/lib/python3.8/lib-dynload', '/app/.heroku/python/lib/python3.8/site-packages']` My Survey page in pages.py looks as follows: class Survey(Page): form_model = 'player' form_fields = [ ] def is_displayed(self): player = self.player return player.treatment == "treatment1" or player.treatment == "treatment2" def get_form_fields(self): if self.player.treatment == "treatment2": if self.player.store_time != 0 and self.participant.vars["payoff1_self"] == 1: return ['q0', 'q1','q_change'] elif self.player.store_time != 0 and self.participant.vars["payoff1_self"] ==10: return ['q0','q1','q_nochange'] elif self.player.store_time == 0 and self.participant.vars["payoff1_self"] ==10: return ['q0','q2','q_change'] elif self.player.store_time == 0 and self.participant.vars["payoff1_self"] == 1: return ['q0','q2','q_nochange'] elif self.player.treatment == "treatment1": if self.player.store_time != 0 and self.participant.vars["payoff1_self"] == 1: return ['q0', 'q1', 'q_nochange'] elif self.player.store_time != 0 and self.participant.vars["payoff1_self"] ==10: return ['q0', 'q1', 'q_change'] elif self.player.store_time == 0 and self.participant.vars["payoff1_self"] ==10: return ['q0', 'q2', 'q_nochange'] elif self.player.store_time == 0 and self.participant.vars["payoff1_self"] == 1: return ['q0', 'q2', 'q_change'] else: pass def before_next_page(self): self.player.set_payoffs() self.player.set_bonus() self.player.set_payoff3() My HTML Survey page simply contains `{% formfields %}`to fetch the dynamic fields. If instead of using `get_form_fields()` I simply specify a form field in `form_fields=[ ]` , the error disappears. Anyone knows what is going on here?
#2
by
Chris_oTree
It could be because some branches of your get_form_fields don't return any value. Try putting 'return []' at the very end of the code.
#3
by
crispi
(edited )
Writting what follows solved the error but did not solve the issue, now I am left with no formfields. Is this what you mean? I might've misunderstood you. def get_form_fields(self): if self.player.treatment == "treatment2": . . . elif self.player.treatment == "treatment1": . . . return []