django-generic-m2m#

Model example#

Consider such a model, using django-generic-m2m to handle generic many-to-many relations:

from django.db import models

from genericm2m.models import RelatedObjectsDescriptor


class TestModel(models.Model):
    name = models.CharField(max_length=200)

    locations = RelatedObjectsDescriptor()

    def __str__(self):
        return self.name

View example#

The Form example works here too: we’re relying on Select2 and QuerySetSequence again.

Form example#

As usual, we need a backend-aware widget that will make only selected choices to render initially, to avoid butchering the database. As we’re using a QuerySetSequence and Select2 for multiple selections, we’ll try QuerySetSequenceSelect2Multiple widget.

Also, we need a field that’s able to use a QuerySetSequence for choices to validate multiple models, and then update the RelatedObjectsDescriptor relations: GenericM2MQuerySetSequenceField.

Finnaly, we can’t use Django’s ModelForm because it doesn’t support non-editable fields, which RelatedObjectsDescriptor is. Instead, we’ll use FutureModelForm.

Example:

class TestForm(autocomplete.FutureModelForm):
    locations = autocomplete.GenericM2MQuerySetSequenceField(
        queryset=autocomplete.QuerySetSequence(
            Country.objects.all(),
            City.objects.all(),
        ),
        required=False,
        widget=autocomplete.QuerySetSequenceSelect2Multiple(
            'location-autocomplete'),
    )

    class Meta:
        model = TestModel
        fields = ('name',)