How to create tuple with key receiving value?

0

I would like to create a method that returns the Django ORM FILTER parameters. I will have to use these filters at different times, I would like to create this generic method. Here's the example:

def home(selected_page=None):
    _config = Config.objects.filter()
    _config = _config.filter(filter_config_by_page(selected_page))
    return _config

def filter_config_by_page(selected_page):
    final_tuple = ()
    if selected_page == "work":
        final_tuple = (works__user__isnull = False,)
    elif selected_page == "not-work":
        final_tuple = (works__user__isnull = True,)
    elif selected_page == "solved":
        final_tuple = (status = 2,)
    elif selected_page == "not-solved":
        final_tuple = (status = 3,)
    return final_tuple

Error

File "/myapp/configuerson/controller/configs.py", line 101
    final_tuple = (works__user__isnull = False,)
                                         ^
SyntaxError: invalid syntax

I believe, according to the error that has appeared, that I can not assign value to tuples that I am creating in this way. Would there be any way to make filter_config_by_page work by following this logic that I described?

    
asked by anonymous 05.02.2018 / 18:22

1 answer

0

Instead of returning a tuple, you can return a dictionary with the parameters you want to use in the filter. Then expand the dictionary using ** :

def home(selected_page=None):
    _config = Config.objects.filter()
    kwargs = filter_config_by_page(selected_page)
    _config = _config.filter(**kwargs)
    return _config

def filter_config_by_page(selected_page):
    final_tuple = {}
    if selected_page == "work":
        final_tuple = {"works__user__isnull": False}
    elif selected_page == "not-work":
        final_tuple = {"works__user__isnull": True}
    elif selected_page == "solved":
        final_tuple = {"status": 2}
    elif selected_page == "not-solved":
        final_tuple = {"status": 3}
    return final_tuple
    
05.02.2018 / 21:03