django-select2 icon indicating copy to clipboard operation
django-select2 copied to clipboard

Feature suggestion: Add ModelSelect2Mixin.result_from_instance() method.

Open sdolemelipone opened this issue 2 years ago • 3 comments

It would be useful to have a way to override or add additional data to the results returned by AutoResponseView for particular widgets.

The use case is processing the select2:select jQuery event on the client side and updating another part of the web page with the related object information. E.g. a ModelChoiceField selects a Product object, and the product's price is rendered alongside the form.

Let me know if this pull request looks acceptable. Thanks

sdolemelipone avatar Mar 21 '24 14:03 sdolemelipone

Oh, and the other use case would be improving performance by using QuerySet.values():

class MyModelWidget(ModelSelect2Widget):
    search_fields = ["name"]
    model = Product

    def filter_queryset(*args, **kwargs):
        return super().filter_queryset(*args, **kwargs).values("pk", "name")

    def result_from_instance(self, obj):
        return {"id": obj["pk"], "text": obj["name"]}

sdolemelipone avatar Mar 21 '24 14:03 sdolemelipone

Hi @sdolemelipone 👋

Thank you for reaching out. I think you are describing a rather common scenario. I always went ahead and wrote custom views that accompany my widgets. However, I do see a point in using reflection, to only write a widget that includes all extension.

Usually, I'd appreciate a discussion or ticket first, but let's move this along anyway. I didn't do a full review yet, but off the top of my head: Please add the request to the methods context (needed to construct fully qualified URLs or permission management). I also need to be cautious with state leakage. Since the widget is passed between requests, modifications to the widget instance, might propagate to other requests.

Cheers! Joe

codingjoe avatar Apr 29 '24 10:04 codingjoe

Hi @codingjoe, thanks for taking the time to look at my suggestion. Noted, next time I'll start with a ticket - I found writing the PR interesting so at least no wasted effort on my part there :-).

I'll add the request to the function arguments as you suggested. Regarding the state leakage, this seems like a problem a user could introduce if they did something unusual in result_from_instance(), which involved previous calls to the method - or is there something I'm missing? Is it something that needs to be mentioned in the documentation?

Thanks Mark

sdolemelipone avatar May 02 '24 11:05 sdolemelipone

Hi @codingjoe, I've added the request parameter as you suggested. Let me know if there's any other changes you'd like to see. Thanks Mark

sdolemelipone avatar May 21 '24 11:05 sdolemelipone

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Project coverage is 98.22%. Comparing base (f61b09c) to head (027edb2). Report is 11 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #272      +/-   ##
==========================================
+ Coverage   98.21%   98.22%   +0.01%     
==========================================
  Files           7        7              
  Lines         280      282       +2     
==========================================
+ Hits          275      277       +2     
  Misses          5        5              

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

codecov[bot] avatar Jun 29 '24 14:06 codecov[bot]

cc @syphar

codingjoe avatar Jun 29 '24 14:06 codingjoe

Great 😃 thanks @codingjoe.

sdolemelipone avatar Jun 30 '24 05:06 sdolemelipone