-
Notifications
You must be signed in to change notification settings - Fork 59
Description
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
- Open
examples/examples/experimental_examples.py. - 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() - Replace
columns__artist__field__include=Truewith:columns__artist__field=dict( include=True, choices=lambda table, instance, **_: exclude_artists(table, instance, _), ), - Go to
localhost:8001/experimental/ - Create a new row, or change any of the existing row values to "New Artist", and hit Save.
- You should get an "Artist matching query does not exist" error.