Home > Software engineering >  How to map API using raw function in django
How to map API using raw function in django

Time:06-01

I want to create a search field in my Django project, but in Django document given for the database, but I want to use this for API/without using the database field

Person.objects.raw('SELECT id, first_name, last_name, birth_date FROM myapp_person')

Anybody could help me? How could this be for API?

CodePudding user response:

You can use ORM for searching rather than using raw, for example: API request body:

{
    'first_name': 'Alex',
    'last_name': 'Joe',
    'birth_date': '01-01-1990'
}

# In your search function
first_name = request.data.get('first_name')
last_name = request.data.get('last_name')
birth_date = request.data.get('birth_date')

persons = Person.objects.filter(first_name__icontains=first_name,
          last_name_icontains=last_name,
          birth_date__contains=birth_date)
  • Related