wagtail / wagtail-generic-chooser

A toolkit for custom chooser popups in Wagtail
BSD 3-Clause "New" or "Revised" License
116 stars 25 forks source link

Select attribute other than id #37

Closed pupattan closed 3 years ago

pupattan commented 3 years ago

I am using DRFChooserViewSet, every time i choose an item id is selected and saved . Is there any way to save other parameters like type or slug

{

    "id": 3,
    "meta": {
        "type": "theme_7.HomePage",
        "detail_url": "http://localhost/cms-api/v2/pages/3/",
        "html_url": null,
        "slug": "new-page",
        "show_in_menus": false,
        "seo_title": "",
        "search_description": "",
        "first_published_at": "2021-09-09T12:04:02.254351+05:30",
        "parent": null
    },
    "title": "New page"
}
gasman commented 3 years ago

If you override the appropriate methods, yes. (See https://github.com/wagtail/wagtail-generic-chooser#customising-chooser-views for an example of overriding.) At minimum, you'll want to override the get_object_id method of DRFChooserMixin to return the desired field: https://github.com/wagtail/wagtail-generic-chooser/blob/ee0ce5ecda891ac5963ac7edc83a7b55ccb6be0c/generic_chooser/views.py#L271-L272

However, if you're intending to use that with a chooser widget, you'll probably need to customise a lot more than that, because when it tries to re-display an already populated widget, it'll try to look up the object by ID so that it can display the object's friendly name - and if you've only stored the slug / type and not the ID, that's not really possible.