[Fixed]-Use custom query result as source for Django model (MySQL database)

1👍

For feeding model with raw SQL query you can use raw() method. Take a look at this example (from official reference):

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)

Read more here

Leave a comment