Skip to content

Field.choices callable on an EditTable doesn't seem to have access to data from the row it's on #658

@smangham

Description

@smangham

Description

Currently, if you have an EditTable with a select column and would like to prevent users from duplicating existing values, you can try and set the callable for field.choices to exclude values already in the table.

"Add Row" will then only show 'new' options... but the table will refuse to save as the limited choice set is also used to validate the existing rows on saving. It's not possible to use the field argument to the choices callable to append the current value to the choice list for rows that already have a value, as field.value is None when the callable is called.

Expected Behaviour

As the choices callable is called per row using AJAX, it suggests data for each row should be available to it. Rows that were not modified were also not expected to be saved.

Reproduction

  1. Open examples/examples/experimental_examples.py.
  2. Insert the following code at the top, to add a new dummy artist to select and code to exclude 'duplicate' artists from the choice set:
    Artist.objects.get_or_create(name="New Artist")
    
    def exclude_artists(table, instance, _):
        return Artist.objects.exclude(
            pk__in=table.rows.values_list('artist', flat=True)
        ).all()
    
  3. Replace columns__artist__field__include=True with:
                columns__artist__field=dict(
                    include=True,
                    choices=lambda table, instance, **_: exclude_artists(table, instance, _),
                ),
    
  4. Go to localhost:8001/experimental/
  5. Create a new row, or change any of the existing row values to "New Artist", and hit Save.
  6. You should get an "Artist matching query does not exist" error.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions