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.12k stars 352 forks source link

ChainedForeignKey should also provide an option by means of which we can target a specific field #310

Open SilverFoxA opened 4 years ago

SilverFoxA commented 4 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. Add the app in Installed apps
  3. Started using the portal

Actual behavior

Inline form don't show any fields.

Expected behavior

Ideal case is that we should be able to see the options.

I'm trying to address the following:

OPTION_TYPE = (
    ('select', 'Select'),
    ('radio', 'Radio'),
    ('checkbox', 'Checkbox')
)
class Option(models.Model):
    id = models.AutoField(db_index=True, primary_key=True)
    type = models.CharField(max_length=32, choices=OPTION_TYPE, default='select')
    sort_order = models.IntegerField(default=0, blank=True)
    name = models.CharField(db_index=True, max_length=128)
    show_image = models.BooleanField(default=True)
class OptionValue(models.Model):
    id = models.AutoField(db_index=True, primary_key=True)
    name = models.CharField(max_length=128)
    image = models.ImageField(null=True, blank=True, upload_to='uploads/optionValue/%Y/%m/%d',
                              validators=[FileExtensionValidator(allowed_extensions=('png', 'jpg', 'jpeg', 'svg'))])
    sort_order = models.IntegerField(default=0, blank=True)

    option_id = models.ForeignKey(
        'catalog.Option',
        db_index=True, on_delete=models.CASCADE,
    )
class ProductOption(models.Model):
    id = models.AutoField(db_index=True, primary_key=True)
    required = models.BooleanField(default=True)

    product_id = models.ForeignKey(
        'catalog.Product', null=True,
        db_index=True, on_delete=models.CASCADE,
    )
    option_id = models.ForeignKey(
        'catalog.Option', null=True,
        db_index=True, on_delete=models.CASCADE,
    )
class ProductOptionValue(models.Model):
    id = models.AutoField(db_index=True, primary_key=True)
    price = models.DecimalField(db_index=True, max_digits=19, decimal_places=4)
    price_prefix = models.CharField(max_length=1, choices=PRODUCT_OPTION_PREFIX, default="+")

    product_option_id = models.ForeignKey(
        'catalog.ProductOption',
        db_index=True, on_delete=models.CASCADE,
    )

    option_value_id = ChainedForeignKey(
        'catalog.OptionValue',
        chained_field="product_option_id",
        chained_model_field="option_id",
        show_all=False,
        auto_choose=True,
        sort=True
    )

Screenshot_5

If you notice this ChainedForeignKey field, we are referring to the model but the ideal chained_field should be option_id from the model ProductOption

option_value_id = ChainedForeignKey(
        'catalog.OptionValue',
        chained_field="product_option_id",
        chained_model_field="option_id",
        show_all=False,
        auto_choose=True,
        sort=True
    )

Is there an option where instead of just defining the entire model we can also refer to a particular field as the chained_field ?

manelclos commented 4 years ago

Hi @SilverFoxA,

We had the same problem when trying to convert a Foreign Key using to_field option, which smart selects does not support.

I'm marking this as a feature request.

SilverFoxA commented 4 years ago

Hello @manelclos,

Have you found any workaround yet? I'm trying to debug but failed to see any request log on the console/network.

manelclos commented 4 years ago

@SilverFoxA after looking carefully at your example, I think that what smart selects needs to implement is following relationships in the chained field, in your case that would be something like:

chained_field="product_option_id__option_id",

That would be an Option model which is a parent of OptionValue. Unfortunately I don't know how difficult this would be to implement.

As a workaround, I think you would be successful by adding a Foreign Key to Option in the ProductOptionValue model, this is untested:

class ProductOptionValue(models.Model):
    id = models.AutoField(db_index=True, primary_key=True)
    price = models.DecimalField(db_index=True, max_digits=19, decimal_places=4)
    price_prefix = models.CharField(max_length=1, choices=PRODUCT_OPTION_PREFIX, default="+")

    product_option_id = models.ForeignKey(
        'catalog.ProductOption',
        db_index=True, on_delete=models.CASCADE,
    )

    option_id = ChainedForeignKey(
        'catalog.Option',
        chained_field="product_option_id",
        chained_model_field="option_id",
        show_all=False,
        auto_choose=True,
        sort=True
    )

    option_value_id = ChainedForeignKey(
        'catalog.OptionValue',
        chained_field="option_id",
        chained_model_field="option_id",
        show_all=False,
        auto_choose=True,
        sort=True
    )
SilverFoxA commented 4 years ago

@manelclos Thank you for your prompt response. I would suggest we look at an approach similar to what you have suggested

chained_field="product_option_id__option_id",

The other workaround which you have pointed is something we want to ignore as that will not work for the instance when the product is being created. There's another approach where we ignore the entire productoption and productionoptionvalue model and merge them in one, but that will be just another workaround.

For now we are going for a quickfix with the formset js, as I have limited time. Hoping to contribute on this issue soon.

charles-co commented 3 years ago

Any update on this yet? currently having the same issue. Are there alternatives, any response will be appreciated. Thanks.

joel-fmjr commented 1 year ago

Any update here? Or alternatives