How could I go about utilizing the Postgres Prefix Plugin within django? Is there a way to just append an additional WHERE clause to the queries that django runs without going into raw SQL?
Maybe something like this?
Model.objects.filter(field1=2, field2__in=[1,2,3]).where("prefix #> '0123456789'")
Yes it is possible, this is the extra method on QuerySet. Something like this should do the trick:
Model.objects.filter(...).extra(where=["prefix #> '0123456789'"])
Related
I have a sql query that works like this, but I couldn't figure out how to write this query in django. Can you help me ?
select datetime,
array_to_json(array_agg(json_build_object(parameter, raw))) as parameters
from dbp_istasyondata
group by 1
order by 1;
You can use raw function of django orm. You can write your query like this:
YourModel.objects.raw('select * from your table'): #---> Change the model name and query
Here you have PostgreSQL system functions. Django ORM doesn't have all these functions. Django ORM can cover it partially.
Now you can do one of these.
Write ''raw sql'' and execute it.
You can write python #staticmethods to implement that functionality then use it in your view.py. (Here you'll have python-power)
First of all, I know that my question duplicate this question. But I supose it's not the same.
I need to save user "search filter". As I understand Django ORM create specific SQL query for different DB. So if I save SQL query I can't migrate on other database with different SQL syntax.
Am I wrong? If no, how can I save Django side of query, without accsesing to DB?
The short answer is that you're correct -- mostly. If the SQL dialect that Django compiled the query for isn't compatible with a different backend, it wouldn't work or might work unpredictably.
To save the Django side of the query, why not just save the actual filter() statement that you're using or a representation of it that you can convert back on the fly?
Edit: Okay in that case I think you're on the right track based on comments and above answer. If you're parsing a query string already save that in the database as a CharField and then just use it to build a Django QuerySet when you retrieve it. If I'm understanding.
If you can suggest better sulution I open for conversation
So... Pickle the function .filter() is not the best idea so as saving SQL string for specific DB. I think the best solution for this problem is saving search parameters. In my case it's GET string. I get it:
request.META["QUERY_STRING"]
And save to DB.
If I need to get it, i just parse:
from django.http import QueryDict
QueryDict(request.META["QUERY_STRING"])
Aditionally I use different form for validate this values (optional) SearchTrustedForm(), because if data structure has changed I can save backwards compatibility.
The following query:
SELECT L1.task_id FROM task_log L1
LEFT JOIN task_log L2 ON L1.started BETWEEN L2.started AND L2.ended
WHERE L2.task_id IS NULL;
Simply reads all task logs that were started when no other tasks were running. An abstraction in PHP/ZF2 looks like this:
$db->select(array('L1' => 'task_log'))
->columns('task_id')
->join(array('L2' => 'task_log'), "L1.started BETWEEN L2.started AND L2.ended", array(), 'left');
I was looking for something similar in Django, or at least python. As per this discussion, it is (understandably) out of question to build this in django models. Are there any other alternatives in python/django?
There seem to be these options:
(A) RawSQL with string templates and maybe some generic model field reflection code to make it a bit more generic.
(B) Django's Expression API which might allow you to re-use some of the Django ORM stuff and only extend it in order to make SELF JOINs work. Though I'm wondering why there wouldn't be some Django module already. Maybe it's too rare of a use case.
Looking up i found this: http://docs.sqlalchemy.org/en/latest/core/tutorial.html which is exactly what i needed.
I have been playing arround with django for a couple of days and it seems great, but I find it a pain if I want to change the structure of my database, I then am stuck with a few rather awkward options.
Is there a way to completely bypass djangos database abstraction so if I change the structure of the database I dont have to guess what model would have generated it or use a tool (south or ...) to change things?
I essentially want this: https://docs.djangoproject.com/en/dev/topics/db/sql/ (Raw SQL Queries) but instead of refering to a model, refering to an external database.
Could I just create an empty model and then only perform raw queries on it? (and set up the DB externally)
Thanks
P.S. I dont really mind if I have separate databases for the admin stuff and the app data
It's in your question already, just read the docs article from here: Executing custom SQL directly
How do I use the Werkzeug framework without any ORM like SQLAlchemy? In my case, it's a lot of effort to rewrite all the tables and columns in SQLAlchemy from existing tables & data.
How do I query the database and make an object from the database output?
In my case now, I use Oracle with cx_Oracle. If you have a solution for MySQL, too, please mention it.
Thanks.
SQLAlchemy supports reflection so you don't have to do that. Take a look at the autoload parameter of Table, you can even make this work with the ORM.
Is it a problem to use normal DB API, issue regular SQL queries, etc? cx_Oracle even has connection pooling biolt in to help you manage connections.
maybe this is what i looking for http://www.sqlalchemy.org/trac/wiki/SqlSoup
and ht*p://spyced.blogspot.com/2006/04/introducing-sqlsoup.html
so i don't have to declare the table to get the object
rp = db.bind.execute('select * from mupp')
a = rp.fetchall()
a[0].name
that's great...thanks for all inspiring response