Saphire
Saphire

Reputation: 1930

Check if object T exists field-wise in queryset of T

I have an address objects in my app which has the usual street, street_nr, etc. fields.

Is there a better way to do this:

Address.objects.filter(street=data["street"],
    street_nr=data["street_nr"],
    zip_code=data["zip_code"],
    city_name=data["city_name"],
    country_name=data["country_name"]).exists()

Mind that I check only for the passed fields.

Upvotes: 0

Views: 109

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 600049

As long as the key names in the data dictionary match the field names you want to query on, you can use the **kwargs syntax:

Address.objects.filter(**data)

Upvotes: 2

Related Questions