django-orm

Pivoting data and complex annotations in Django ORM

自闭症网瘾萝莉.ら 提交于 2020-01-12 06:55:11
问题 The ORM in Django lets us easily annotate (add fields to) querysets based on related data, hwoever I can't find a way to get multiple annotations for different filtered subsets of related data. This is being asked in relation to django-helpdesk, an open-source Django-powered trouble-ticket tracker. I need to have data pivoted like this for charting and reporting purposes Consider these models: CHOICE_LIST = ( ('open', 'Open'), ('closed', 'Closed'), ) class Queue(models.model): name = models

How to recursively query in django efficiently?

喜夏-厌秋 提交于 2020-01-11 05:09:33
问题 I have a model, which looks like: class StaffMember(models.Model): id = models.OneToOneField(to=User, unique=True, primary_key=True, related_name='staff_member') supervisor = models.ForeignKey(to='self', null=True, blank=True, related_name='team_members') My current hierarchy of team is designed in such a way that there is let's say an Admin (who is at the top most point of hierarchy). Now, let's say 3 people (A, B, C) report to Admin and each one of A, B and C have their own team reporting

Django __in lowercase

北慕城南 提交于 2020-01-10 04:26:05
问题 I'm using django-taggit, which handles the attachment of tags to arbitrary content types. I imported a large tag list, which contains many uppercase words, as well as lowercase words. Now, I' trying to get objects of another class containing a set of tags, but I want to compare case insensitively. When I do this: Media.objects.filter(tags__name__in=['tag1', 'tag2']) objects containing e.g. the tag "Tag1" are not found, only those ones with "tag1" or "tag2". Is there any possibility in the

MySQL not creating indexes for foreign key

大兔子大兔子 提交于 2020-01-06 06:09:30
问题 I read that InnoDB automatically creates indexes for Foreign-key. Does MySQL Workbench automatically create indexes for foreign keys? Does MySQL index foreign key columns automatically? https://dev.mysql.com/doc/refman/5.6/en/innodb-foreign-key-constraints.html But some of my foreign keys do not have the index in the table. Check pharmaceutical_id foreign-key field. It does not have an index. | pharmaceuticals_pharmaceuticalcode | CREATE TABLE `pharmaceuticals_pharmaceuticalcode` ( `id` int

Django distinct returns more records than count

时光总嘲笑我的痴心妄想 提交于 2020-01-06 04:41:17
问题 The following shows the distinct count is 2247 In [6]: VirtualMachineResources.objects.all().values('machine', 'cluster') ...: .distinct().count() Out[6]: 2247 But then when I loop through it, it returned way more than it should: In [4]: a = [] ...: for resource in VirtualMachineResources.objects.all().values('mach ...: ine', 'cluster').distinct(): ...: if resource['cluster']: ...: a.append(resource['cluster']) ...: ...: In [5]: len(a) Out[5]: 96953 Given the records have cluster field being

Django: efficient filtering in the ORM across ForeignKeys?

北城余情 提交于 2020-01-06 01:46:27
问题 I'm trying to construct a moderately complex filter in the Django ORM, and I'm not sure if I could do it more efficiently. My app lets users search for shoes in a particular size. So if they select a size (or multiple sizes), I look for all the shoes available in that size. This is my model structure. There's an object for each shoe ( Shoe ), an object for each size ( ShoeSize ) (standardised across manufacturers), and an object ( ShoeSizeAvailable ) that only gets created if a particular

Specifying order_by on Related Field in Django

我的未来我决定 提交于 2020-01-05 19:17:59
问题 In Django, on a given model it looks like the RelatedManager may only be set statically (see: docs) but is there any way to set the order_by on the related manager for a given Queryset? I tried using prefetch_related and Prefetch , but that does not seem to work: qs = qs.prefetch_related( Prefetch('item_set', queryset=Item.objects.order_by('capture_dt').all())) res = qs.first().item_set.all().ordered In this case res is False . The problem comes when I try to access the related manager:

Does the ORM call init when retrieving a saved instance?

你说的曾经没有我的故事 提交于 2020-01-05 04:11:31
问题 I'm doing a migration changing a nullable field to be not-nullable. The new __init__ routine ensures that fields can't be null by doing some custom callisthenics to come up with a suitable default. The question is whether it is essential to migrate the existing data to apply the new rules for a default, or will those rules be applied automagically whenever a legacy object is retrieved? Reading the source I suspect the ORM restores a pickle of the saved data, thus I would need to update all

How to save a django model with a manyToMany Through relationship, AND regular manyToMany relationships

匆匆过客 提交于 2020-01-05 03:43:43
问题 I've read everything I can find on ManyToMany relationships in Django, but so far I'm not seeing anything that solves my specific problem of having a ManyToMany-Through relationship alongside a simple ManyToMany that Django handles so effortlessly on its own. Consider these models: class Treatment(models.Model): ... book = models.ManyToManyField(Book, through='TreatmentLocation') category = models.ManyToManyField(Category) class Book(models.Model): name = models.CharField(...) class

Can't set character_set_results to latin1

帅比萌擦擦* 提交于 2020-01-05 03:04:39
问题 I've decided to use Django 1.7 for the first time with Python 3. I need to be able to work with legacy latin1 database which contains utf8 data. I know it sucks, but the database is so huge that it's really impossible to change this. So I tried following: DATABASES = { 'ENGINE' : 'django.db.backends.mysql', // using MySQL-python fork with support for py3 ... 'OPTIONS' : { 'init_command': "SET character_set_results = 'latin1'", #'read_default_file': '/etc/my.cnf.d/client.cnf', // I've also