django-orm

How to select many to one to many without hundreds of queries using Django ORM?

只愿长相守 提交于 2019-12-06 10:12:44
My database has the following schema: class Product(models.Model): pass class Tag(models.Model): product = models.ForeignKey(Product) attr1 = models.CharField() attr2 = models.CharField() attr3 = models.CharField() class AlternatePartNumber(models.Model): product = models.ForeignKey(Product) In other words, a Product has many Tag s, and a Product has many AlternatePartNumber s. Tag s are a collection of attributes of the Product . Given the three attributes in a Tag , I want to select the associated Product s that match (could be more than one), as well as all of the AlternatePartNumber s of

Django Call Stored Procedure on Second Database

故事扮演 提交于 2019-12-06 08:52:03
I'm trying to call a stored procedure on a multi-db Django installation, but am not having any luck getting results. The stored procedure (which is on the secondary database) always returns an empty array in Django, but the expected result does appear when executed in a mysql client. My view.py file from SomeDBModel import models from django.db import connection def index(request, someid): #Some related django-style query that works here loc = getLocationPath(someid, 1) print(loc) def getLocationPath(id, someval): cursor = connection.cursor() cursor.callproc("SomeDB.spGetLocationPath", [id,

Timedelta between two fields

社会主义新天地 提交于 2019-12-06 06:05:56
问题 I'm looking for objects where the timedelta between two fields is greater than a certain number of days. Baiscally I have a date when a letter is sent, and a date when an approval is received. When no approval is received in let's say 30 days, then these objects should be included in the queryset. I can do something like the below, where the delta is something static. However I don't need datetime.date.today() as a start but need to compare against the other object. delta = datetime.date

Django multidb: write to multiple databases

怎甘沉沦 提交于 2019-12-06 04:34:33
问题 With Django multidb, it's fairly easy to write a router that runs a master/slave infrastructure. But is it possible to write a router that writes to multiple databases? My use case is a collection of projects, all running on the same domain. To save users from registering/login in on every site, I'd like to synchronize the contrib.auth and contrib.sessions tables. Is that possible with Django multidb or should I look into replication features of the database system (MySQL in my case)? 回答1: I

Django ORM: filter by hour range

ⅰ亾dé卋堺 提交于 2019-12-06 03:55:32
I'm trying to implement a filter for hour range, it should returns records with a date between hourA and hourB (ie: "give me the records saved between 16pm and 18pm"). My attempts: 1) Using new 1.6 __hour filter and __in or __range : MyModel.objects.filter(date__hour__in=(16, 17, 18)) MyModel.objects.filter(date__hour__range=(16, 18)) The code above generates exceptions 2) Using Q objects: hList = [Q(date__hour=h) for h in (16, 17, 18)] MyModel.objects.filter(reduce(operator.or_, hList)) This version works, but is very inefficient, since for each hour in the range it repeats the extract() call

Can I control the GROUP BY in django 1.3's orm?

旧街凉风 提交于 2019-12-06 03:29:09
问题 I think this will best be explained with an example. Here is what the data would looks like: |project | |id|name | |1 |some project | |2 |my other project| |run | |id|project_id|start_time |result | |1 |1 |1305732581845|something| |2 |1 |1305732593721|nothing | |3 |2 |1305732343721|nothing | |4 |2 |1305732556821|something| I would like to be able to get an entire recordset from each of the latest runs by project. The SQL Query would look something like this: SELECT *, MAX("run"."start_time")

Django performance testing suite that'll report on metrics (db queries etc.)

本小妞迷上赌 提交于 2019-12-06 01:59:04
问题 I have a complex Django web application that has many person-years of work put into it. It might need optimisation sometime. There are several common operation/flows that I could script with (say) django's test client. Is there some programme that, given a python script like that, will run then, and report on various django specific performance metrics, like 'number of sql queries run'. Essentially something like a unittest test suite, but rather than reporting "0 tests failed", it'd report

Django query for many-to-many subset containment

て烟熏妆下的殇ゞ 提交于 2019-12-05 23:17:38
问题 Is there a way to query for subset or superset containment with many-to-many fields? Suppose each Person has a list of birds they want to see, and each Aviary houses a list of birds. How can I make a query to find, for a given Person instance, which Aviaries have every bird on the person's list? And similarly, for a given Person instance, how do I find which Aviaries have only birds on the person's list (but not necessarily all of them). Here are my Django 1.5 models: class Bird(models.Model)

Django annotate and values(): extra field in 'group by' causes unexpected results

喜欢而已 提交于 2019-12-05 20:06:51
I must be missing something obvious, as the behavior is not as expected for this simple requirement. Here is my model class: class Encounter(models.Model): activity_type = models.CharField(max_length=2, choices=(('ip','ip'), ('op','op'), ('ae', 'ae'))) cost = models.DecimalField(max_digits=8, decimal_places=2) I want to find the total cost for each activity type. My query is: >>> Encounter.objects.values('activity_type').annotate(Sum('cost')) Which yields: >>> [{'cost__sum': Decimal("140.00"), 'activity_type': u'ip'}, {'cost__sum': Decimal("100.00"), 'activity_type': u'op'}, {'cost__sum':

Single django queryset to get n adjacent items

可紊 提交于 2019-12-05 18:24:45
I'm making an "infinite"/continuous scrolling list (like twitter) and want to be able to navigate to a specific item. Finding the item is straight forward, but I need to get a few items before and after it. I'm using a similar approach to the answers suggested here: Getting next and previous objects in Django before = list(reversed(models.MyModel.objects.filter(pk__lt=pk).order_by('-pk')[:10])) after = list(models.MyModel.objects.filter(pk__gte=pk).order_by('pk')[:11]) objects = before + after Is there any way to combine these into the one query? I can't simply combine the two with the |