python - Django using custom SQL instead of models to return JSON object to template -


i able retrieve json data/object models.py , mysql database , send json object template. how able use custom sql retrieve data mysql make json object send template. don't want use models.py @ all. here have in views.py when using models.py:

def startpage(request):     platforms = platform.objects.select_related().values('platformtype')     return render(request, 'html1.html', {'platforms_as_json' : json.dumps(list(platforms)),}) 

this have far:

def my_custom_sql(self):     cursor = connection.cursor()     cursor.execute("select platformtype platform", [self.platform])     row = cursor.fetchone()     return row 

how able same thing except without using models.py , using custom sql queries within views? thank you

update:

def startpage(request):     platforms = my_custom_sql()     return render(request, 'html1.html', {'platforms_as_json' : json.dumps(list(platforms)), })   def my_custom_sql():     cursor = connection.cursor()     cursor.execute("select hwplatformname hwplatform", none)     rows = cursor.fetchall()     return rows 

now able data onto template don't believe giving me correct json format..

if want instances of model, you're looking raw method on objects property.

platforms = platform.objects.raw('select * platform') 

if you're looking content server, can return value sql query:

platforms = my_custom_sql() # or call my_custom_sql statically. 

if you're looking delayed population, can put yield statement my_custom_sql function.


Comments