Commits

Luke Plant committed 5742a1e

Simplified Filter internal API and gave ForeignKeyFilter a choice_from_param method consistent with other filters

Comments (0)

Files changed (1)

django_easyfilters/filters.py

         chosen = self.chosen
         choices = []
         for choice in chosen:
-            display_obj = self.display_object_from_choice(choice)
-            if display_obj is None:
-                continue
-            choices.append(FilterChoice(self.render_choice_object(display_obj),
+            choices.append(FilterChoice(self.render_choice_object(choice),
                                         None, # Don't need count for removing
                                         self.build_params(remove=[choice]),
                                         FILTER_REMOVE))
         return choices
 
-    def display_object_from_choice(self, choice):
-        """
-        Converts a raw 'choice' (derived from the query string)
-        into an object that will be displayed.
-
-        The 'choice' object passed could be a string, but some subclasses
-        convert to a more convenient type of object.
-
-        This method allows subclasses to do expensive conversion work (e.g. that
-        requires a DB lookup).
-
-        If it returns 'None', the 'remove' link will not be displayed.
-        """
-        return choice
-
     def render_choice_object(self, choice_obj):
         """
         Converts an object that is available for choosing (that usually is the
         out = []
         for i, choice in enumerate(chosen):
             to_remove = [c for c in chosen if c >= choice]
-            display_obj = self.display_object_from_choice(choice)
-            if display_obj is None:
-                continue
-            out.append(FilterChoice(self.render_choice_object(display_obj),
+            out.append(FilterChoice(self.render_choice_object(choice),
                                     None,
                                     self.build_params(remove=to_remove),
                                     FILTER_REMOVE))
     """
     Filter for ForeignKey fields.
     """
-    def display_object_from_choice(self, choice):
-        lookup = {self.rel_field.name: choice}
+    def choice_from_param(self, param):
+        choice_pk = super(ForeignKeyFilter, self).choice_from_param(param)
+        lookup = {self.rel_field.name: choice_pk}
         try:
             obj = self.rel_model.objects.get(**lookup)
         except self.rel_model.DoesNotExist:
-            return None
+            raise ValueError("object does not exist in DB")
         return obj
 
     def get_choices_add(self, qs):
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.