django-queryset

Use of full-text search + GIN in a view (Django 1.11 )

Deadly 提交于 2019-12-10 18:53:33
问题 I need some help with building proper query in a django view for full-text search using GIN index. I have quite a big database (~400k lines) and need to do a full-text search on 3 fields from it. Tried to use django docs search and this is code BEFORE GIN. It works, but takes 6+ seconds to search over all fields. Next I tried to implement a GIN index to speed up my search. There are a lot of questions already how to build it. But my question is - how does the view query change when using a

Filtering based on .count() of a forgein key field in Django querysets

别来无恙 提交于 2019-12-10 18:38:11
问题 So I have some Django 1.3 models like this: class Type(models.Model): is_bulk = models.BooleanField() class Component(models.Model): parent = models.ForeignKey(Type) Some Type 's have 0 Component 's, some have 1, or 2, etc. How do I write a QuerySet that filters all Type's that have > 0 Components. i.e. exclude Types that have 0 Components? 回答1: from django.db.models import Count Type.objects.annotate(component_count=Count('component')).exclude(component_count=0) 来源: https://stackoverflow.com

Django - Annotating Weighted AVG by Group

天大地大妈咪最大 提交于 2019-12-10 18:32:51
问题 I've got the following model manager that operates on a SalesRecord: def by_variety_and_date(self, start_date, end_date): return self.model.objects.filter( date__range=(start_date, end_date) ).values( "variety" ).annotate( qty_applied=Sum('qty_applied'), margin=Avg('margin') ) What I'd really like is for the margin=Avg('margin') to return a weighted average, based on qty_applied . Is there a way to do this with Django's annotate/aggregate queries? I've been experimenting with stringing

Different databases with the same models on Django

我的未来我决定 提交于 2019-12-10 17:38:50
问题 I have the following issue: I need a different database with the same models for each user (or set of users). I have a way of finding out to which database the user is related. The issue is I always have to use the using method on every query I make. For example: Thing.objects.using('appropriate_database').all() Is there a way to avoid the use of using and making the user/database relationship implicit somehow? 回答1: Sounds like a bad design that can't scale to me. You have to duplicate the

How to aggregate over a single queryset in Django?

╄→尐↘猪︶ㄣ 提交于 2019-12-10 17:37:00
问题 Short description : given a queryset myQueryset , how do I select max("myfield") without actually retrieving all rows and doing max in python? The best I can think of is max([r["myfield"] for r in myQueryset.values("myfield")]) , which isn't very good if there are millions of rows. Long description : Say I have two models in my Django app, City and Country. City has a foreign key field to Country: class Country(models.Model): name = models.CharField(max_length = 256) class City(models.Model):

Dynamically build complex queries with Q() in Django [closed]

假如想象 提交于 2019-12-10 17:12:14
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 6 years ago . First example: # ANDing Q objects q_object = Q() q_object.add(Q(), Q.AND) # ORing Q objects q_object = Q() q_object.add(Q(), Q.OR)

Combine prefetch_related and annotate in Django

*爱你&永不变心* 提交于 2019-12-10 16:27:40
问题 I have three models class ModelA(models.Model): name = CharField(max_length=100) class ModelB(models.Model): modela = ForeignKey(ModelA) class ModelC(models.Model): modelb = ForeignKey(ModelB) amount = IntegerField() I can get the output name, number of model c objects ============== Some name, 312 Another name, 17 With the queryset ModelA.objects.all().prefetch_related('modelb_set', 'groupb_set__modelc_set') and template {% for modela in modela_list %} {% for modelb in modela.modelb_set.all

Django - Filter a queryset by Max(date) year

旧城冷巷雨未停 提交于 2019-12-10 15:38:54
问题 I would like to know if I can get in a single query, All the objects of certain model where its date's year equals the year of the Max('date') of the model. For example, using the models from the Aggregation Django Docs, how can I get All the Book s published in the year of the more recently published Book ? All the examples in the docs filter by immediate values ( pubdate__year=2006 ), but I need to use a calculated value over the same object in the same query. Of course, I could do this by

Django query with simple arithmetic among model fields and comparison with field from another model

烂漫一生 提交于 2019-12-10 15:32:50
问题 (All code below is a simplified representation of the actual code). If I have the following Django models: - class Material(models.Model): name = models.CharField(max_length=110) class OrderDetail(models.Model): material = models.ForeignKey(Material) order_quantity = models.IntegerField() quantity_delivered = models.IntegerField(null=True, blank=True) is_open = models.BooleanField() is_active = models.BooleanField() class MaterialRequirement(models.Model): material = models.ForeignKey

Is there any way to do a case-insensitive IN query in Django?

可紊 提交于 2019-12-10 15:31:18
问题 Nearly every kind of lookup in Django has a case-insensitive version, EXCEPT in, it appears. This is a problem because sometimes I need to do a lookup where I am certain the case will be incorrect. Products.objects.filter(code__in=[user_entered_data_as_list]) Is there anything I can do to deal with this? Have people come up with a hack to work around this issue? 回答1: I worked around this by making the MySQL database itself case-insensitive. I doubt that the people at Django are interested in