django-orm

Django prefetch_related children of children

十年热恋 提交于 2019-12-11 00:45:41
问题 I have a model Node that looks something like that: class Node(models.Model): parent = models.ForeignKey('self', related_name='children', on_delete=models.CASCADE) A Node can have several children, and each of these children can have its own children. If I do: def show_child(node): for child in node.children.all(): show_child(child) root_node = Node.objects.prefetch_related('children').get(pk=my_node_id) # hit database twice, as expected print("Now testing queries") root_node.children.all() #

Using dateadd in django filter

喜欢而已 提交于 2019-12-10 23:41:39
问题 I have a model that defines subscription periods by start date and duration (in days): class SubscriptionProduct(models.Model): start_date = models.DateField() duration = models.IntegerField() I need to be able to filter subscriptions that are currently active, e.g. start_date < now < start_date+duration I can't find the django way to do it. I can use raw SQL statements that use postgres' DATEADD equivalent of INTERVAL but i'd prefer to use something builtin and non db specific. I assume

Django ORM orderby exact / prominent match to be on top

点点圈 提交于 2019-12-10 23:15:50
问题 I need to order the results based on the length of match in Django ORM. I have a Suburb table with location details in name field. I have a requirement to search the table with given text and order by exact match / most prominent match to be the top For example: 1) if search string is 'America' then the result should be [America, South America, North America ..] in this case we found a complete match, which has to be the first element. 2) if search is port then the result should be ['Port

Django: SELECT … FROM ONLY table

荒凉一梦 提交于 2019-12-10 22:33:41
问题 Can Django do SELECT ... FROM ONLY ... queries without using .raw() ? I need it to omit archived tables in my queries to Postgres. 回答1: I just did a hg grep on 'ONLY' in django/db of the development version. No hits, so no. What's the bigger picture? Why not use .raw()? 来源: https://stackoverflow.com/questions/7733437/django-select-from-only-table

Django model inheritance, filtering models

不想你离开。 提交于 2019-12-10 22:09:12
问题 Given the following models:(don't mind the TextFields there're just for illustration) class Base(models.Model): field1 = models.TextField() class Meta: abstract=True class Child1(Base): child1_field = models.TextField() class Child2(Base): child2_field = models.TextField() class Content(models.Model): aso_items = models.ManyToManyField('Base') According to these definitions a Content object can be associated with more than one Base object, eg. an interview(=Content object) can be linked with

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 Transaction managed block ended with pending COMMIT/ROLLBACK

你说的曾经没有我的故事 提交于 2019-12-10 18:14:44
问题 I have a view function which needs to be manually transaction managed, but when I apply the @transaction.commit_manually decorator, django ALWAYS raises the below exception. As you can see from the code trace below, the transaction is committed right before return from the view. I am using sqlite, both on windows and linux, with django 1.4. The following is the output of django_trace, followed by the exception. To be clear: this happens whether or not I use django_trace, and when there are no

Related objects reference with custom manager

丶灬走出姿态 提交于 2019-12-10 17:19:30
问题 I want to get the related object references and I want to use a custom manager. Is there something outside? How I can use a custom manager to get these objects? b.entry_set.all() E.g b.custom_manager.entry_set.all() b.entry_custom_manager_set.all() 回答1: It looks like a ticket is still open for this feature https://code.djangoproject.com/ticket/3871 来源: https://stackoverflow.com/questions/16301532/related-objects-reference-with-custom-manager

Django Aggregation Count

北战南征 提交于 2019-12-10 16:36:17
问题 I'm trying to filter my model with an aggregate function. I have a Model A and a Model B with a foreign key on the model A. annotate_pool = queryset.annotate(nb_bets=Count('bets')).all() for obj in annotate_pool: bets_obj = obj.bets.all() bets_length = len(bets_obj) print(obj.nb_bets, bets_length) And the annotation doesn't give me the same result as the function length. 1 1 1 2 1 2 1 2 1 2 1 1 1 1 2 2 Here is my models: class Pronostic(models.Model): cote_total = models.FloatField(default=0

How to pass a list of values for an `in` SQL clause in Django

最后都变了- 提交于 2019-12-10 16:34:40
问题 I need to use a raw SQL query in a Django app I am writing. The SQL query contains an in clause in the where statement: select * from abc_mymodel where some_fk in (1,2,3) and some_status = 'foo' I am a big proponent of passing SQL params as parameters. This is easily done for the single value ones. However, I am not sure how (or if) this can be done for in clauses. Ideally I would like to do something like: sql = """ select * from abc_mymodel where some_fk in %s and some_status = %s """ my