django-orm

Get top n records for each group with Django queryset

ぃ、小莉子 提交于 2021-01-07 01:24:11
问题 I have a model like the following Table, create table `mytable` ( `person` varchar(10), `groupname` int, `age` int ); And I want to get the 2 oldest people from each group. The original SQL question and answers are here StackOverflow and One of the solutions that work is SELECT person, groupname, age FROM ( SELECT person, groupname, age, @rn := IF(@prev = groupname, @rn + 1, 1) AS rn, @prev := groupname FROM mytable JOIN (SELECT @prev := NULL, @rn := 0) AS vars ORDER BY groupname, age DESC,

Get top n records for each group with Django queryset

杀马特。学长 韩版系。学妹 提交于 2021-01-07 01:23:45
问题 I have a model like the following Table, create table `mytable` ( `person` varchar(10), `groupname` int, `age` int ); And I want to get the 2 oldest people from each group. The original SQL question and answers are here StackOverflow and One of the solutions that work is SELECT person, groupname, age FROM ( SELECT person, groupname, age, @rn := IF(@prev = groupname, @rn + 1, 1) AS rn, @prev := groupname FROM mytable JOIN (SELECT @prev := NULL, @rn := 0) AS vars ORDER BY groupname, age DESC,

Get top n records for each group with Django queryset

喜欢而已 提交于 2021-01-07 01:23:45
问题 I have a model like the following Table, create table `mytable` ( `person` varchar(10), `groupname` int, `age` int ); And I want to get the 2 oldest people from each group. The original SQL question and answers are here StackOverflow and One of the solutions that work is SELECT person, groupname, age FROM ( SELECT person, groupname, age, @rn := IF(@prev = groupname, @rn + 1, 1) AS rn, @prev := groupname FROM mytable JOIN (SELECT @prev := NULL, @rn := 0) AS vars ORDER BY groupname, age DESC,

Django ORM filter SUM different related objects on a loop

ぃ、小莉子 提交于 2021-01-01 06:38:56
问题 I have the following models: class Developer(models.Model): name = models.CharField(max_length=100) class Skill(models.Model): code = models.CharField(max_length=30) class Experience(models.Model): date_from = models.DateField(blank=True, null=True) date_to = models.DateField(blank=True, null=True) developer = models.ForeignKey(Developer, related_name='experience', on_delete=models.CASCADE) class SkillExperience(models.Model): skill = models.ForeignKey(Skill, on_delete=models.CASCADE, related

Django ManyToMany all values by default

前提是你 提交于 2020-12-16 05:31:09
问题 I have the following model: class Product(models.Model): provinces = models.ManyToManyField('Province', related_name='formats') By default, products can be sold in every province. How can I define the model "Product" so that every product created has all provinces by default? Thanks! 回答1: Use the default key. You can't directly set default model values to an iterable like a list, so wrap them in a callable, as the Django documentation advises: https://docs.djangoproject.com/en/1.8/ref/models

Django ORM: window function with subsequent filtering

夙愿已清 提交于 2020-08-22 12:00:26
问题 Answering this question, I found out that window functions are not allowed to combine with filter (technically, they are, but filter clause affects the window). There is a hint to wrap window function in an inner query, so that final SQL looks like this (as I understand): SELECT * FROM ( SELECT *, *window_function* FROM TABLE) WHERE *filtering_conditions* The question is: how can I write this query with Django ORM? 回答1: Another solution is Common Table Expressions (CTE), and with the help of

Bulk delete Django by ids

天涯浪子 提交于 2020-07-20 04:51:22
问题 I writing a project using Django REST Framework, Django and Postgres as a database. I want to bulk delete in one query. So, it is possible to do without writing a query using pure SQL? There is an example, but the count of executet query equal length of a list of ids (for example, if in delete_ids 2 ids, Django will execute 2 queries): delete_ids = [...] MyModel.objects.filter(id__in=delete_ids).delete() 回答1: Not possible using the filter and delete together using raw sql query. https://docs