django-orm

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

ⅰ亾dé卋堺 提交于 2019-12-22 09:37:52
问题 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")

django-tastypie - How to make manytomany through relationship

我与影子孤独终老i 提交于 2019-12-22 04:43:29
问题 I'm working on a API for a project and I have a relationship Order/Products through OrderProducts like this: In catalog/models.py class Product(models.Model): ... In order/models.py class Order(models.Model): products = models.ManyToManyField(Product, verbose_name='Products', through='OrderProducts') ... class OrderProducts(models.Model): order = models.ForeignKey(Order) product = models.ForeignKey(Product) ... Now, when I load an Order through the API I'd like to get the related Products as

How to select_related on reverse foreign key? [duplicate]

本秂侑毒 提交于 2019-12-22 03:09:52
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: A left outer reverse select_related in Django? A BlogPost has many Comment s. I want to get a list of BlogPost s and all their comments. Thus, I have BlogPost.objects.filter(my_filter).select_related() But the ForeignKey is on the Comment , not the BlogPost , so the select_related() doesn't prefetch any comments. Is there a way to get this to work? I can't reverse the query ( Comment.objects... ) because then

Django ORM. Joining subquery

左心房为你撑大大i 提交于 2019-12-21 21:28:12
问题 I have a table which contains list of some web sites and a table with statistics of them. class Site(models.Model): domain_name = models.CharField( max_length=256, unique=True, ) class Stats(models.Model): date = models.DateField() site = models.ForeignKey('Site') google_pr = models.PositiveIntegerField() class Meta: unique_together = ('site', 'date') I want to see all sites and statistics for a concrete date. If a stats record for the date doesn't exist, then the selection must contain only

Django ORM misreading PostgreSQL sequences?

て烟熏妆下的殇ゞ 提交于 2019-12-21 21:24:29
问题 Background : Running a PostgreSQL database for a Django app (Django 1.1.1, Python2.4, psycopg2 and Postgres 8.1) I've restored the database from a SQL dump several times. Each time I do that and then try to add a new row, either shell, admin, or site front end, I get this error: IntegrityError: duplicate key violates unique constraint "app_model_pkey" The data dump is fine and is resetting the sequences. But if I try adding the row again, it's successful! So I can just try jamming a new row

Django: Sum on an date attribute grouped by month/year

試著忘記壹切 提交于 2019-12-21 16:47:27
问题 I'd like to put this query from SQL to Django: "select date_format(date, '%Y-%m') as month, sum(quantity) as hours from hourentries group by date_format(date, '%Y-%m') order by date;" The part that causes problem is to group by month when aggregating. I tried this (which seemed logical), but it didn't work : HourEntries.objects.order_by("date").values("date__month").aggregate(Sum("quantity")) 回答1: aggregate can only generate one aggregate value. You can get the aggregate sum of Hours of the

Django Many to Many Relationship Add Not Working

蓝咒 提交于 2019-12-21 06:34:53
问题 I'm using Django's ManyToManyField for one of my models. class Requirement(models.Model): name = models.CharField(max_length=200) class Course(models.Model): requirements = models.ManyToManyField(Requirement) I want to be able to assign requirements for my classes, so to do that, I try the following: I get a class, course, that is already saved or that I have just saved, and I run the following: c = Course.objects.get(title="STACK 100") req = Requirement.objects.get(name="XYZ") c.requirements

Django Many to Many Relationship Add Not Working

妖精的绣舞 提交于 2019-12-21 06:34:14
问题 I'm using Django's ManyToManyField for one of my models. class Requirement(models.Model): name = models.CharField(max_length=200) class Course(models.Model): requirements = models.ManyToManyField(Requirement) I want to be able to assign requirements for my classes, so to do that, I try the following: I get a class, course, that is already saved or that I have just saved, and I run the following: c = Course.objects.get(title="STACK 100") req = Requirement.objects.get(name="XYZ") c.requirements

How to add filters to a query dynamically in Django?

回眸只為那壹抹淺笑 提交于 2019-12-21 05:08:27
问题 In my viewSet I am doing a query, queryset= Books.objects.all(); Now from an ajax call I get my filter values from UI i.e. age,gender, etc. of auther.There will be a total of 5 filters. Now the problem which I ran into is how am I going to add filters to my query(only those filters which have any value). What I tried is I checked for individual filter value and did query, but that way it fails as if the user remove the filter value or add multiple filters. Any better suggestion how to

How to add filters to a query dynamically in Django?

牧云@^-^@ 提交于 2019-12-21 05:08:02
问题 In my viewSet I am doing a query, queryset= Books.objects.all(); Now from an ajax call I get my filter values from UI i.e. age,gender, etc. of auther.There will be a total of 5 filters. Now the problem which I ran into is how am I going to add filters to my query(only those filters which have any value). What I tried is I checked for individual filter value and did query, but that way it fails as if the user remove the filter value or add multiple filters. Any better suggestion how to