I try to delevop a CMS with django. It is necessery to add to each article a responsibilty maxtrix for each user.
settings.py:
RESPONSIBILITY_CHOICES = {
"H": "Hauptverantwortlich",
"N": "Nebenverantwortlich",
"X": "Mitarbeit",
"-": "Nichts",
}
class UserResponsibilityModel(models.Model):
def get_user_responsibility():
return {i: i for i in settings.RESPONSIBILITY_CHOICES}
responsibility = models.CharField(max_length=5,choices=get_user_responsibility)
user = models.OneToOneField(User, on_delete=models.CASCADE)
content_id = models.ForeignKey(ContentModel, on_delete=models.CASCADE)
def __str__(self):
return self.user.username
class UserResponsibleForm(forms.ModelForm):
def get_user_responsibility():
return {i: i for i in settings.RESPONSIBILITY_CHOICES}
responsibility = forms.ChoiceField(choices=get_user_responsibility())
class Meta:
model = UserResponsibilityModel
fields = ['user','responsibility','content_id']
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields['user'].disabled = False
self.fields['user'].widget = forms.TextInput(attrs={'readonly': 'readonly'})
self.fields['user'].queryset = User.objects.all()
if self.instance and self.instance.pk:
self.fields['user'].initial = self.instance.username
self.fields['user'].label = 'Username'
self.fields['user'].help_text = None
self.fields['content_id'].queryset=ContentModel.objects.all()
my template to Create the responsibiliies template:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Formset</title>
</head>
<body>
<form method="post" enctype="multipart/form-data">
{% csrf_token %}
{{ formset.management_form }}
<table>
{% for hidden in form.hidden_fields %}
{{ hidden }}
{% endfor %}
{% for form in formset %}
<tr>
<td>{{ form.user.label_tag }}</td>
<td>{{ form.user }}</td>
<td>{{ form.responsibility }}</td>
<td>{{ form.content_id }}</td>
</tr>
{% endfor %}
</table>
<input type="submit" value="Submit">
</form>
</body>
</html>
If I want to save the model I get to following error messages:
<option value="3">Gregor Stanek</option>
</select>
<input type="hidden" name="form-1-id" id="id_form-1-id">
</div><ul class="errorlist nonfield"><li>(Hidden field id) This field is required.</li></ul>
<div>
<label for="id_form-2-user">Username:</label>
<ul class="errorlist"><li>Select a valid choice. That choice is not one of the available choices.</li></ul>
<input type="text" name="form-2-user" value="cstanek" readonly="readonly" aria-invalid="true" id="id_form-2-user">
I dont find the error .
Thanks in Advance for the help