0

I have a function like this...which helps me with code re-writing when calling out a query

def user_profile(self, **kwargs):
    default_fields = {
        'is_deleted': False,
        'is_staff': False,
        'is_active': False
    }
    kwargs.update(default_fields)

    return Profile.objects.filter(**kwargs)

but let's say, if I don't want to add another new parameter into the function and I want to override the is_staff field sometimes *maybe out of 20 queries only 1 need is_staff: True.

Is an easy way?

I have thought of adding another parameter into the function to detect if True / False something like that which would work.

But I wonder if there's an even easier faster way to do this?

Thanks in advance for any suggestions.

Tsuna
  • 2,098
  • 6
  • 24
  • 46
  • Here you can find more information about merge two dicts http://stackoverflow.com/questions/38987/how-to-merge-two-python-dictionaries-in-a-single-expression#26853961 – bomba1990 May 17 '17 at 02:17

1 Answers1

1

Try this:

def user_profile(self, **kwargs):
    default_fields = {
        'is_deleted': False,
        'is_staff': False,
        'is_active': False
    }
    default_fields.update(kwargs)

    return Profile.objects.filter(**default_fields)

Basically just turn your default_fields dict into the base dict so if you want to override 'is_staff', just pass is_staff via kwargs.

munsu
  • 1,914
  • 19
  • 24