[Django]-How to write a raw SQL query in Django QuerySet?

0👍

In order to get the result back in QuerySet format you need to start with a Djano model, for example:

class Person(models.Model):
    first_name = models.CharField(...)
    last_name = models.CharField(...)
    birth_date = models.DateField(...)

>>> for p in Person.objects.raw('SELECT * FROM myapp_person'):
        print(p)

For more detail, visit the Django documentation at: Django Documentation

Leave a comment