-
Notifications
You must be signed in to change notification settings - Fork 39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add multiselect for M2M relationships in forms #235
Comments
I agree, this would be nice. It's definitely something we should add at some point. The work around at the moment is to register the joining table with Piccolo Admin: # Where GenreToBand is the joining table:
create_admin(tables=[Band, Genre, GenreToBand]) This means you can edit the M2M relationships. However, it would be slicker if it was all on the same page. |
M2M widgets usually look something like this (taken from here): We could do something similar (unless we can think up a better design). |
@dantownsend After a lot of experimentation I was able to use multiselect to add and edit m2m columns on a single page. I used the Vue-Multiselect library (I also tried Vue-multiselect-listbox but couldn't get it to work). Currently it is possible to add multiple m2m columns per table, reverse lookup in PiccoloCRUD for all rows and single row. Changes are required in all three Piccolo repositories. The smallest change is in Pydantic utils in Piccolo ORM, and the bigger in Piccolo API and Piccolo Admin. If you like this I can make PRs in all three repositories so you can see what it's all about and because this is a big task it would be good if you could help me with this. Screencast.2023-03-26.21.02.55.mp4 |
@sinisaos It looks promising. What changes are required on the Piccolo / Piccolo API side? With the widget, is that doing a search on the backend? Or are all of the options being pulled into memory at once? I wonder if we can reuse the array widget somehow, which means we don't have to introduce another library? |
@dantownsend In Piccolo ORM, the change is only in Pydantic utils. I set the format and list type for the M2M columns (so we can use it as an array in Piccolo Admin later). Here's the change. # in utils/pydantic.py
for column in table._meta.m2m_relationships: # type: ignore
column_name = column._meta.name
field = pydantic.Field(format="m2m", extra=extra, **params)
columns[column_name] = (t.List[t.Any], field)
|
@sinisaos It's quite a big change, so probably best to do it in stages. I wonder if it's best to add M2M support to You can send me the code if you want via email, but it's easier to see the diffs in GitHub. |
@dantownsend That's exactly why I wanted to email the changes so that you can see what they're all about (I think it's easier than opening PRs that may not be good at all, and to make things work we first have to make changes in Piccolo ORM, then in Piccolo API and finally in Piccolo Admin. I hope it makes sense why I wanted to email you the changes in the first place). |
@dantownsend Also branches links if that's easier for you on Github. |
Hello @sinisaos This (the code you demoed in your screencast) is exactly what I need to build a list of ForeignKeys when adding new rows to one of my table. Do you still have the code stashed somewhere and would you mind sharing it again? Fantastic work in any case, super useful functionality. Cheers, |
@zindy Thanks for the kind words. Unfortunately, I don't have that code anywhere, but even if I had it, it wouldn't be valid because it was made for Piccolo versions before v1 (when Piccolo used Pydantic v1, and now it uses Pydantic v2). Also Piccolo Admin now uses Vue3, not Vue2. I see that vue-multiselect has a version for Vue3, so I'll try to do something similar. If I manage to make anything, I'll be happy to share it here. |
@sinisaos thank you for the quick reply! I'm very new to all this and hadn't realised so much work had been done on Piccolo (-admin) in the last 6 months. It makes sense that the old code wouldn't probably even load. But don't worry, I'm not blocked by the absence of this feature: If I must encode some examination result as determined by say "a junior pathologist alone" vs "a senior pathologist" vs "a junior + AI tools", I'll just use two foreign keys, reviewer1 and reviewer2 and then do some kind of combination of reviewer1+reviewer2 on row creation. For now, that's more than enough. Multiselect will be "good to have" because it'll be a lot more flexible, but I'm not greedy. Anyway, I'll let everyone know what works and what doesn't for my application (building a clinical database of a research project) and if there is anything I can help with or interesting code I've developed, I'll share it in relevant threads! Cheers, |
@zindy Somehow I managed to get the same functionality with the latest versions of Piccolo (I left some comments in the code so you can use this more easily). Here are all the branches. |
It would be really nice if the forms to add/edit items would have support to include a field that is in a many to many relationship. The existing UI for foreign key relationships would mostly work, except that it would have to support selecting multiple items.
The text was updated successfully, but these errors were encountered: