Help creating preferences on the fly
See original GitHub issueI am trying to create a queryset filter on the fly per model and per user.
All my ListViews inherit from a class that has a filters(self) property. In that section I want to create a preference for queryset filters using django-filter module.
I have found several issues with my approach:
def filters(self):
self.filter_name = self.model.__name__ + "FilterSet"
self.filter_preference_name = "%s_%s_filter" % (self.model._meta.app_label, self.filter_name.lower())
self.filter_preference_full_name = "Personal__%s_%s_filter" % (self.model._meta.app_label, self.filter_name.lower())
self.filter_preference_class_name = "FilterPreference%s%s" % (self.model._meta.app_label.title(), self.filter_name)
if self.filter_preference_full_name not in preferences_manager.keys():
FilterPref = type(str(self.filter_preference_class_name), (StringPreference,), {
'section':personal,
'name':self.filter_preference_name,
'default': '{}',
'instance': self.request.user,
})
user_preferences_registry.register(FilterPref)
Some times preference is created but not persistent. When I change from /myob/list to /anotherobj/list myobj list disappears. Some times preference is not created at first time, and is created after a second reload.
I also have tried creating preference using orm like in tests and does not persist: UserPreferenceModel.objects.get_or_create( instance=self.henri, section="misc", name='is_zombie')[0].value)
I would not like to create a dynamic_preferences_registry files and objects for more than 30 models, plus I am planning to set settings also for tables: per_page and list_mode (table, cards, …)
Any ideas why my approach is wrong? is it possible?
Thanks in advance.
Issue Analytics
- State:
- Created 6 years ago
- Comments:12 (12 by maintainers)
I proceed to close the issue. Thank you so much, Eliot!
But I suspect your problem here is that you try to register preferences dynamically after your server started. This is maybe possible, but not explicitely supported by this project: you have to declare preferences statically (you can use inheritance and mixins, though) so they are picked up and loaded during the apps loading.
You can do that, and still use that dynamical approach of yours to filter preferences in a view, though.