jazzband / django-smart-selects

chained and grouped selects for django forms
https://django-smart-selects.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
1.1k stars 348 forks source link

ChainedManytoManyField not working with inline forms #336

Open raftorres opened 2 years ago

raftorres commented 2 years ago

You MUST use this template when reporting issues. Please make sure you follow the checklist and fill in all of the information sections below.


All versions of django-smart-selects prior to version 1.2.8 are vulnerable to an XSS attack as detailed in issue 171. As a result, all previous versions have been removed from PyPI to prevent users from installing insecure versions. All users are urged to upgrade as soon as possible.

Checklist

Put an x in the bracket when you have completed each task, like this: [x]

Steps to reproduce

  1. Git clone the master to a app directory
  2. Run migrations and load data json
  3. Started test_app and use the portal admin
  4. Try add new membership inside groups

Actual behavior

ChainedManytoManyField not working with inline forms

Expected behavior

Tested with Python 3.8 / Django 3.2 / django-smarts-selects last version 1.5.9

Code in models.py

# Test many to many with inlines and formsets
class Person(models.Model):
    name = models.CharField(max_length=128)

    def __str__(self):
        return "%s" % self.name

class Group(models.Model):
    name = models.CharField(max_length=128)
    members = models.ManyToManyField(Person, through="Membership")

    def __str__(self):
        return "%s" % self.name

class Talent(models.Model):
    name = models.CharField(max_length=64)
    persons = models.ManyToManyField(Person, blank=True)

    def __str__(self):
        return "%s" % self.name

class Membership(models.Model):
    person = models.ForeignKey(Person, on_delete=models.CASCADE)
    group = models.ForeignKey(Group, on_delete=models.CASCADE)
    talents = ChainedManyToManyField(
        Talent, chained_field="person", chained_model_field="persons", horizontal=True
    )
    date_joined = models.DateField()

Code admin.py

class MembershipInline(admin.TabularInline):
    model = Membership
    extra = 1

class PersonAdmin(admin.ModelAdmin):
    list_display = ("name",)

class GroupAdmin(admin.ModelAdmin):
    inlines = (MembershipInline,)

class TalentAdmin(admin.ModelAdmin):
    list_display = ("name",)

When I try to add a new membership, it does not load relationship data within inline forms when selecting person

image

image

ChainedForeingKey works, but ChainedManytoManyField fails. The console and network do not throw any errors.

Please, any solution?