by
0
4
6,915
122
Top 1% !
Popular
Pearl of Wisdom
Specified
Popularity: 955th place
Created
Modified Jan 29, 2016

Published on:

No tags for this snippet yet.
LanguagePython
SourceGitHub

Django - convert RawQuerySet to QuerySet

Django - convert RawQuerySet to QuerySet: 
raw_as_qs.py
Copy Embed Code
<iframe id="embedFrame" style="width:600px; height:300px;"
src="https://www.snip2code.com/Embed/1031353/Django---convert-RawQuerySet-to-QuerySet?startLine=0"></iframe>
Click on the embed code to copy it into your clipboard Width Height
Leave empty to retrieve all the content Start End
from django.db import connection, models class MyManager(Manager): def raw_as_qs(self, raw_query, params=()): """Execute a raw query and return a QuerySet. The first column in the result set must be the id field for the model. :type raw_query: str | unicode :type params: tuple[T] | dict[str | unicode, T] :rtype: django.db.models.query.QuerySet """ cursor = connection.cursor() try: cursor.execute(raw_query, params) return self.filter(id__in=(x[0] for x in cursor)) finally: cursor.close() class MyModel(models.Model): objects = MyManager()
If you want to be updated about similar snippets, Sign in and follow our Channels