Python Forum

Full Version: Creating few models with one submit depending on the selected items from multiselect?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
A'm beginner at Django. Sry for my bad english. I'm ukrainian)

There are 2 models: 1.RelationType; 2. RequestRelation. The second model is connected with the first through MTM field.

There is a multiple select form based on a list of “relationship types”. Need to create few models of when RequestRelation submitting, depending on the selected items of the multi-select?

F.e., *if "husband / wife / son" is chosen, need to create 3 models accordingly*. Distinctive in them are only the types of relationships.

The form.is_valid(), the correct values ​​come.

But so far, only one model is created, with lists of all selected relationship types.

Also in my code there is a bug. The model is created immediately when the page is loaded, not when it is submitted.

Thank you in advance.

    class RelationType(models.Model):
        title = models.CharField(max_length=40)
        def __unicode__(self):
            return self.title
    class RelationRequest(models.Model):
        creator = models.ForeignKey(User, related_name='creator')
        relation = models.ForeignKey(User, related_name='relation')
        type_of_relation = models.ManyToManyField(RelationType, related_name='type_relation',
        status = models.BooleanField(_('status'), default=False)
        created = models.DateTimeField(_('created'), auto_now_add=True)
        updated = models.DateTimeField(_('updated'), auto_now=True)

    <form action="" method="POST" multiple="multiple">
        {% csrf_token %}
        {{ relation_form.type_of_relation }}
        <input type='submit' value='ok'>
    class RelationRequestForm(forms.ModelForm):
        class Meta:
            model = RelationRequest
            fields = ('type_of_relation',)
            widgets = {
                'type_of_relation': forms.SelectMultiple(
                        'class': 'select2',
                        'style': 'width: 235px',
        def __init__(self, *args, **kwargs):
            super(RelationRequestForm, self).__init__(*args, **kwargs)
            self.fields['type_of_relation'].empty_label = None
            self.fields['type_of_relation'] = forms.ModelMultipleChoiceField(queryset=RelationType.objects.all())
        def clean(self):
            type_of_relation = self.cleaned_data.get('type_of_relation')

    def post(self, request, *args, **kwargs):
            self.object = self.get_object()
            relation_form = RelationRequestForm(request.POST)
            if relation_form.is_valid():
                req_rel =
                req_rel.creator = request.user
                relation_user_id = int(filter(lambda x: x.isdigit(), request.path))
                req_rel.relation = User.objects.get(id = relation_user_id)
            context = self.get_context_data(relation_form = relation_form)
            return self.render_to_response(context)