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

Empty results when accessing field in `chained_field`. #346

Open YDA93 opened 1 year ago

YDA93 commented 1 year ago

Checklist

Steps to reproduce

class Service(models.Model):
  ...

class ServiceOption(models.Model):
    service = models.ForeignKey(
        Service,
        on_delete=models.PROTECT,
        related_name="options",
        verbose_name=_("Service"),
    )

class PartnerService(models.Model):
    service = models.ForeignKey(
        Service,
        on_delete=models.PROTECT,
        related_name="partners",
        verbose_name=_("Service"),
    )

class PartnerServiceOption(models.Model):
    service = models.ForeignKey(
        PartnerService,
        on_delete=models.PROTECT,
        related_name="options",
        verbose_name=_("Service"),
    )

    option = ChainedForeignKey(
        ServiceOption,
        chained_field="service__service",
        chained_model_field="service",
        auto_choose=True,
        on_delete=models.PROTECT,
        related_name="partners",
        verbose_name=_("Service Option"),
    )

Actual behavior

I end up with no results

Expected behavior

Should return selected_service.options.all()

HoliSimo commented 1 year ago

Have you some results? I'm facing the same problem. I'm forced to add some unwanted foreign key in the model with ChainedForeignKey.

YDA93 commented 1 year ago

Hello @HoliSimo

Unfortunately this is not possible in django-smart-select. And as a result, I stopped using it.

HoliSimo commented 1 year ago

@YDA93 Did you find something else to handle this use case?

YDA93 commented 1 year ago

@HoliSimo tbh I have not found any other library better than this one.

If you really like the use case, you can use jquery:

  1. Create a view and url to query the objects and make it for admins only.
  2. Add JS file to related admin page.
  3. Add two functions one to update options and one to make Ajax request.

I know this might sounds a lot but its not and all the code can be re-used on other pages.

amitv9493 commented 1 year ago

I am also having the same issue.