I have multiple forms of the same base class like this:
class DatabaseForm(forms.Form):
active = forms.BooleanField(required=False) # Determines, if username and password is required
username = forms.CharField(label="Username", required=False)
password = forms.CharField(label="Password", required=False)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields['active'].label = self.label
def clean(self):
cleaned_data = super(DatabaseForm, self).clean()
active = cleaned_data.get("ative")
username = cleaned_data.get("username")
password = cleaned_data.get("password")
if active and not (username and password):
raise forms.ValidationError("Please fill in the required database configuration")
return cleaned_data
class MySQLForm(DatabaseForm):
label = "MySQL"
class PostgreSQLForm(DatabaseForm):
label = "PostgreSQL"
class SqliteForm(DatabaseForm):
label = "Sqlite3"
So only if the user chooses a database, he must fill in the username and password for it.
In my template, the form looks like this:
<form action="." method="post">
{% csrf_token %}
{{ mysql_form }}
{{ postgres_form }}
{{ sqlite_form }}
<a id="database-submit-btn" href="#submit"
class="btn btn-success submit-button">Submit</a>
</form>
Problem: When the user chooses a database by selecting the 'active' field, the 'active' fields of the other databases get set to True. It seems like I can't distinguish between the multiple fields, as they have the same key in the POST request (but I'm not sure that's the whole problem): 'username': ['mysql test', 'postgres test', 'sqlite test'], 'password': ['mysql password', 'postgres password', 'sqlite password']
How can I get this to work and keep the code extendable?
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…