django-queryset

Django Many To Many intersection filtering

那年仲夏 提交于 2019-12-10 03:50:51
问题 For the sake of simplicity let's say I only have 2 models: Book, Author class Author(models.Model): name = models.CharField(max_length='100') ... class Book(models.Model): name = models.CharField(max_length='100') authors = models.ManyToManyField(Author) ... I want to filter Books using a list of authors. What I tried to do is: authors = [...] # a list of author objects Books.objects.filter(authors__in=authors) But here, the authors will be ORed when I want them ANDed. Is there any way to AND

Django conditional annotation

一世执手 提交于 2019-12-09 16:24:02
问题 I'm surprised that this question apparently doesn't yet exist. If it does, please help me find it. I want to use annotate (Count) and order_by, but I don't want to count every instance of a related object, only those that meet a certain criteron. To wit, that I might list swallows by the number of green coconuts they have carried: swallow.objects.annotate(num_coconuts=Count('coconuts_carried__husk__color = "green"').order_by('num_coconuts') 回答1: This should be the right way. swallow.objects

In Django, Can I `defer()` fields in an object that's being queried by `select_related()`

让人想犯罪 __ 提交于 2019-12-09 10:59:24
问题 In my Django app I want to use select_related() on a QuerySet to "follow" a ForeignKey field, but I only need to access a few of the fields on the "followed" model instance. Can I use the defer() method somehow with my "followed" field. e.g., if I have... class BarModel(models.Model): ... blah = models.TextField() class FooModel(models.Model): bar = models.ForeignKey(BarModel) ... ...and I'm doing FooModel.objects.all().select_related('bar') how can I defer() the field blah . Thanks. 回答1:

Django query select distinct by field pairs

爷,独闯天下 提交于 2019-12-09 05:42:16
问题 I have the field 'submission' which has a user and a problem. How can I get an SQL search result which will give a list of only one result per user-problem pair? Models are like this: class Problem(models.Model): title = models.CharField('Title', max_length = 100) question = models.TextField('Question') class Submission(models.Model): user = models.ForeignKey(User) problem = models.ForeignKey(Problem) solution = models.CharKey() time = models.DateTimeField('Time', auto_now_add=True) 回答1:

Django-queryset join without foreignkey

馋奶兔 提交于 2019-12-09 05:18:48
问题 model.py class Tdzien(models.Model): dziens = models.SmallIntegerField(primary_key=True, db_column='DZIENS') dzienrok = models.SmallIntegerField(unique=True, db_column='ROK') class Tnogahist(models.Model): id_noga = models.ForeignKey(Tenerg, primary_key=True, db_column='ID_ENERG') dziens = models.SmallIntegerField(db_column='DZIENS') What I want is to get id_noga where dzienrok=1234. I know that dziens should be dziens = models.ForeignKey(Tdzien) but it isn't and I can't change that. Normally

return datetimes in the active timezone with a django query

假如想象 提交于 2019-12-09 03:56:49
问题 I am trying to retrieve the last n hour rows from a table and print their datetimes in a given timezone, the timezone to use when printing dates is given, I am trying to use activate to make django return the datetimes with the proper timezone but it returns dates as UTC. here is my current code: min_time = datetime.datetime.now(link.monitor.timezone) - datetime.timedelta(hours=period) timezone.activate(link.monitor.timezone) rows = TraceHttp.objects.values_list('time', 'elapsed').filter(time

How can filter parent based on children in django

蓝咒 提交于 2019-12-08 17:02:26
问题 I have this from django.db import models class Kid(models.Model): name = models.CharField(max_length=200) class Toy(models.Model): name = models.CharField(max_length=200) owner = models.ForeignKey(Kid) I have this queryset kids = Kid.objects.all() Now i want to filter kids whole toys has name star in it and i am not able to decide which filter to apply kids.filter(toys_set__icontains='star') 回答1: Kid.objects.distinct().filter(toy__name__icontains='star') Note the distinct() method. It is

How do I query the length of a Django ArrayField?

偶尔善良 提交于 2019-12-08 16:14:15
问题 I have an ArrayField in a model, I'm trying to annotate the length of this field ( so far without any luck) F('field_name__len') won't work since join is not allowed inside F() . Even ModelName.objets.values('field_name__len') is not working Any idea? I'm using django 1.11 回答1: The extra() function has been deprecated according to the docs: Use this method as a last resort This is an old API that we aim to deprecate at some point in the future. Use it only if you cannot express your query

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

Django: Filtering Queryset From Two Model Fields

拜拜、爱过 提交于 2019-12-08 13:37:46
问题 My model has two fields (latitude and longitude) that I want to combine to form a point object. However, I cannot figure out how to filter based on a combination of those values: For example: >>> from django.contrib.gis.geos import Point >>> lat = 5 >>> lon = 1 >>> pnt = Point(lat, lon) >>> buf = pnt.buffer(0.0001) >>> z = Thing.objects.filter(pnt__intersects=buf) FieldError: Cannot resolve keyword 'pnt' into field. ## I dont have a model field named pnt I realize this is not the right