django-orm

how does CONN_MAX_AGE work in Django

我与影子孤独终老i 提交于 2019-12-10 14:33:39
问题 can someone ELI5 what CONN_MAX_AGE does? I thought it worked like this: 1) Request #1 comes in, opens connection 1 to database 2) Request #1 uses connection 1 to do some work 3) Request #1 completes. Because CONN_MAX_AGE is non-zero (and the age has not been reached), the connection is left open. 4) Request #2 comes in, and Django re-uses connection #1 to the database. But that doesn't seem to be happening. I have a page on my site that does an AJAX poll every 15 seconds. In my development

Temporary models in django

≡放荡痞女 提交于 2019-12-10 13:54:46
问题 In one celery task I need to create temporary table in database. In this article Daniel Roseman explained how to create one. But this solution does not work in Django 1.9. I tried to look into Django docs and Google but I was unable to find anything useful. Code from mentioned article which worked in Django 1.8: from django.db import models, cursor from django.contrib.contenttypes.management import update_contenttypes from django.core.management import call_command class TempCustomerAddress

Can we do a Sum on CharField in Django ORM?

末鹿安然 提交于 2019-12-10 08:01:01
问题 My model in Django ORM is this class Test(Modelbase): id = models.IntegerField(null=True, blank=True) amount = models.CharField(max_length=255) I want to add the amount for list of id's. The only problem is the amount field is CharField . How do I apply sum for the amount field? Test.objects.filter(id__in=[1,2,3]).aggregate(Sum('amount')) I am using Django=1.9.1 for this. 回答1: you can try do annotate with cast: from django.db.models import FloatField from django.db.models.functions import

Always True Q object

廉价感情. 提交于 2019-12-10 02:31:56
问题 I want to create some part of Django ORM filter query dinamicly, now I can do: if some: Obj.filter( some_f1=some_v1, f1=v1, f2=v2, f3=v3, f4=v4, ... ) else: Obj.filter( f1=v1, f2=v2, f3=v3, f4=v4, ... ) I want something without code duplicate like this: Obj.filter( Q(some_f1=some_v1) if some else True, # what to use instead of True? f1=v1, f2=v2, f3=v3, f4=v4, ... ) 回答1: Here's a hacky way to get an always true Q object: always_true = ~Q(pk=None) This depends on the fact that the primary key

Django query expression for calculated fields that require conditions and casting

旧时模样 提交于 2019-12-10 01:33:49
问题 I am trying to run an aggregation query that is roughly equals to: select sum(impressions) as impressions, sum(clicks) as clicks, sum(clicks)/sum(impressions) as ctr from stats group by product order by ctr; The database used is PostgreSQL. I made this query expression (Django 1.9): Stats.objects.values('product').annotate( impressions = models.Sum('impressions'), clicks = models.Sum('clicks'), ctr = models.ExpressionWrapper( models.F('clicks')/models.F('impressions')), output_field = models

Trying to make a PostgreSQL field with a list of foreign keys in Django

谁都会走 提交于 2019-12-10 01:11:54
问题 Here is what I'm trying to do: Make a model in Django that is a PostgreSQL array (database specific type), which contains foreign keys to another model. class Books(models.Model): authors = ArrayField( models.ForeignKey('my_app.Authors', default=None, null=True, blank=True), blank=True, default=list() ) When I try to makemigrations, Django gives me this error: SystemCheckError: System check identified some issues: ERRORS: my_app.Books.authors: (postgres.E002) Base field for array cannot be a

Django filter through multiple fields in a many-to-many intermediary table

隐身守侯 提交于 2019-12-10 01:10:02
问题 I have the following models in my django project: class Video(models.Model): media = models.ForeignKey(Media) class Media(models.Model): title = models.CharField(max_length=255) formats = models.ManyToManyField(Format,through='MediaFormat',related_name='media',blank=True) class Format(models.Model): title = models.CharField(max_length=50) class MediaFormat(models.Model): status = models.IntegerField() format = models.ForeignKey(Format) media = models.ForeignKey(Media) Now, I want to filter

Is there a downside to using “.filter().filter().filter()…” in Django?

断了今生、忘了曾经 提交于 2019-12-08 22:00:56
问题 Are the following two calls resolved to the equivalent SQL query in Django? Chaining multiple calls Model.objects \ .filter(arg1=foo) \ .filter(arg2=bar) \ ... Wrapping all the args together: Model.objects \ .filter(arg1=foo, arg2=bar) I'd like code to be readable (there are MANY more filter calls than I've shown), but only if there's no sacrifice to performance. 回答1: Update: Disregard this answer. See this better, correct answer. Thanks @Sam for the heads up. Old Answer: Are the following

Django Model field : Ordered List of Foreign Keys

☆樱花仙子☆ 提交于 2019-12-08 17:24:44
问题 I have a Route model which should store an ordered list of stops along that route. How should I go about modeling this relation? class Stop(models.Model): name = .. latitude = .. longitude = .. class Route(models.Model): stops_list = # Ordered list of stops on the route 回答1: Since there are many Stops along a Route, and stops could belong to multiple routes, I would use a ManyToMany to store this relationship. You may specify a through model to store data about the relationship, such as what

Does Django natively support common table expressions?

一个人想着一个人 提交于 2019-12-08 15:31:02
问题 To clarify my question I would like to know if it is possible to idiomatically use the Django ORM whilst accessing CTE features. I imagine I could use CTE by writing raw SQL statements but the ability to use the ORM 'syntactic sugar' to bypass hand coding SQL statements was one of the original appeals of Django. 回答1: Django doesn't support CTEs directly as these are not common to all databases (MySQL doesn't support it). There are packages that extend the capability of Django's ORM to support