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.11k stars 348 forks source link

Creating a ChainedOneToOneField #230

Open SrdjanCosicPrica opened 6 years ago

SrdjanCosicPrica commented 6 years ago

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]

I tried finding a duplicate issue but I was surprised that I did not find one so it may be a possible duplicate.

Steps to reproduce

  1. Create class that needs a OneToOneField and be Chained.

Actual behavior

The FruitDestination class has the function: "All fruit of this type in this plantation should be sent to this destination"


class Plantation(models.Model):
    name = models.CharField(max_length=255)

class Plant(models.Model):
    plantation = models.ForeignKey(Plantation, related_name='plant', on_delete=models.CASCADE)

class Fruit(models.Model):
    plant = models.ForeignKey(Plant, related_name='fruit', on_delete=models.CASCADE)

class FruitDestination(models.Model):
    plantation = models.ForeignKey(Plantation, related_name='fruit_criteria', on_delete=models.CASCADE)
        destination = models.CharField(max_length=255)
        fruit = ChainedForeignKey(
            Fruit,
                chained_field='plantation',
            chained_model_field='plant__plantation',
            show_all=False,
            auto_choose=True,
            sort=False,
            unique=True,
            related_name='fruit_destination',
            on_delete=models.CASCADE

In python manage.py shell

>>>from fruit.models import Fruit
>>> Fruit.objects.all().first().fruit_destination.all().first()
<FruitDestination: FruitDestination object>

Expected behavior


class Plantation(models.Model):
    name = models.CharField(max_length=255)

class Plant(models.Model):
    plantation = models.ForeignKey(Plantation, related_name='plant', on_delete=models.CASCADE)

class Fruit(models.Model):
    plant = models.ForeignKey(Plant, related_name='fruit', on_delete=models.CASCADE)

class FruitDestination(models.Model):
    plantation = models.ForeignKey(Plantation, related_name='fruit_criteria', on_delete=models.CASCADE)
        destination = models.CharField(max_length=255)
        fruit = ChainedOneToOneField(
            Fruit,
                chained_field='plantation',
            chained_model_field='plant__plantation',
            show_all=False,
            auto_choose=True,
            sort=False,
            related_name='fruit_destination',
            on_delete=models.CASCADE
>>>from fruit.models import Fruit
>>> Fruit.objects.all().first().fruit_destination
<FruitDestination: FruitDestination object>