Skip to content

Conversation

@berycz
Copy link
Collaborator

@berycz berycz commented Jun 27, 2025

this PR is open for suggestions/changes
I don't really like the method names nor really the code, since it's quite similar to things inside edit_table__post_handler. But refactoring the code might be a "one day in the future" task

We need to be able inside choices to get the current data of edit table, so we can offer only choices that aren't already selected.
E.g. you have a nested form of a Project + EditTable for team of people working on it, so people shouldn't be selectable twice.
So with this methods I could do something like

@staticmethod
def fields__person__choices(table, **_):
    if (received_data := table.get_received_rows_data(method='GET')) and (row_nr := table.get_row_pk_for_choices()) is not None:
        exclude_pks = []
        for data in received_data:
            if data['pk'] != row_nr and data['person'] is not None:
                exclude_pks.append(data['person'].pk)
        return Person.objects.exclude(pk__in=exclude_pks).all()
    else:
        return Person.objects.all()

I'm having one issue tho - it works for new rows, but not for the existing ones... existing rows get Person.objects.all() for some reason. I'm guessing edit_form doesn't reload choices in bind_field_from_instance

Btw this should be possible to use also inside initial for default values of a new row

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant