Skip to content

.DualSortableSelector incorrectly showing all available items as selected #153

@jamiecash

Description

@jamiecash

When using the DualSortableSelector with a many-to-many relationship and a through table, the database is populated correctly, however the selector displays all available options on the right-hand side when there is more than one instance of the parent model.

Using the following model as an example:
models.py

class Lesson(models.Model):
    name = models.CharField(max_length=100)
    
    class Meta:
      db_table = 'lessons_lesson'

class Course(models.Model):
   name = models.CharField(max_length=100)
   lessons = SortableManyToManyField(
      Lesson, 
      through="CourseLesson",
      verbose_name="lessons in the course")

   class Meta:
      db_table = 'lessons_course'

class CourseLesson(models.Model):
   course = models.ForeignKey(Course, on_delete=models.CASCADE)
   lesson = models.ForeignKey(Lesson,  on_delete=models.CASCADE)
   lesson_number = models.BigIntegerField(default=0)

   class Meta:
      ordering = ['lesson_number']

And the following form:
forms.py

class CourseForm(forms.ModelForm):
    class Meta:
        model = models.Course
        fields = ['name', 'lessons']
        widgets = {
            'lessons': widgets.DualSortableSelector(),
        }

When you add a lesson to a course, this displays correctly. However, when you create a new course and add a lesson to it, both courses display both lessons.

When querying the database. The select statement returns the correct results. It appears that the widget is displaying all lessons in the through table, not just those assigned to the course.

I attempted to recreate using the test app however there are a few missing dependencies that I couldn't get to resolve, notably the .js files that do not appear to be included in static.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions