django-smart-selects icon indicating copy to clipboard operation
django-smart-selects copied to clipboard

Empty results when accessing field in `chained_field`.

Open YDA93 opened this issue 2 years ago • 5 comments

Checklist

  • [X] This issue is not about installing previous versions of django-smart-selects older than 1.2.8. I understand that previous versions are insecure and will not receive any support whatsoever.
  • [ ] I have verified that that issue exists against the master branch of django-smart-selects.
  • [X] I have searched for similar issues in both open and closed tickets and cannot find a duplicate.
  • [ ] I have debugged the issue to the smart_selects app.
  • [X] I have reduced the issue to the simplest possible case.
  • [ ] I have included all relevant sections of models.py, forms.py, and views.py with problems.
  • [ ] I have used GitHub Flavored Markdown to style all of my posted code.

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()

YDA93 avatar Nov 26 '22 22:11 YDA93

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

HoliSimo avatar Dec 08 '22 13:12 HoliSimo

Hello @HoliSimo

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

YDA93 avatar Dec 08 '22 13:12 YDA93

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

HoliSimo avatar Dec 08 '22 13:12 HoliSimo

@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.

YDA93 avatar Dec 08 '22 13:12 YDA93

I am also having the same issue.

amitv9493 avatar Jan 04 '23 06:01 amitv9493