django-queryset

Django query with variable number of filter arguments

久未见 提交于 2021-02-07 18:07:14
问题 I have a Django query that fetches from MyModel based on certain conditions: if beta: MyModel.object.filter(x=alpha, y=beta) else: MyModel.object.filter(x=alpha) Is it possible to eliminate the if beta: check and do it in a single line i.e. make the query filter on y only when beta is not None Is this a good (Djangonic) way: MyModel.object.filter(**{'x':alpha, 'b':beta} if beta else **{'x':alpha}) Or is it possible to do something like this (I know the following is wrong, but can it be fixed

Django query with variable number of filter arguments

左心房为你撑大大i 提交于 2021-02-07 18:04:22
问题 I have a Django query that fetches from MyModel based on certain conditions: if beta: MyModel.object.filter(x=alpha, y=beta) else: MyModel.object.filter(x=alpha) Is it possible to eliminate the if beta: check and do it in a single line i.e. make the query filter on y only when beta is not None Is this a good (Djangonic) way: MyModel.object.filter(**{'x':alpha, 'b':beta} if beta else **{'x':alpha}) Or is it possible to do something like this (I know the following is wrong, but can it be fixed

Django query with variable number of filter arguments

ε祈祈猫儿з 提交于 2021-02-07 18:04:16
问题 I have a Django query that fetches from MyModel based on certain conditions: if beta: MyModel.object.filter(x=alpha, y=beta) else: MyModel.object.filter(x=alpha) Is it possible to eliminate the if beta: check and do it in a single line i.e. make the query filter on y only when beta is not None Is this a good (Djangonic) way: MyModel.object.filter(**{'x':alpha, 'b':beta} if beta else **{'x':alpha}) Or is it possible to do something like this (I know the following is wrong, but can it be fixed

How do I sort a Django QuerySet by an external value?

百般思念 提交于 2021-02-07 14:25:30
问题 I have a dict made up of (id, rank) pairs. I'd like to perform a Django query on the ids such that the resultant queryset is ordered by rank (descending). Getting the queryset is easy: rankings = {...} result = MyModel.objects.filter(id__in=rankings.keys()) It seems like the answer should involve some sort of annotation that I can use as part of the order_by but I can't figure out how to get there. EDIT: I neglected to mention that I need the result to be a QuerySet as this is part of a

Where to put common queries in Django?

浪尽此生 提交于 2021-02-07 13:22:54
问题 I have a queryset that is reasonably complicated, which I currently use in a single view for getting a list of objects. I want to use the same queryset in a couple of other views but would prefer not to just copy the code multiple times. I could use a Manager, to keep the queryset in one place, and use that in each view except the query relies on a date which is different on each page. As I understand it, Managers don't let you pass in variables... so I'm wondering where I should put this

Proper way to annotate a rank field for a queryset

折月煮酒 提交于 2021-02-07 10:22:25
问题 Assume models like this: class Person(models.Model): name = models.CharField(max_length=20) class Session(models.Model): start_time = models.TimeField(auto_now_add=True) end_time = models.TimeField(blank=True, null=True) person = models.ForeignKey(Person) class GameSession(models.Model): game_type = models.CharField(max_length=2) score = models.PositiveIntegerField(default=0, blank=True) session = models.ForeignKey(Session) I want to have a queryset function to return total score of each

Proper way to annotate a rank field for a queryset

[亡魂溺海] 提交于 2021-02-07 10:21:13
问题 Assume models like this: class Person(models.Model): name = models.CharField(max_length=20) class Session(models.Model): start_time = models.TimeField(auto_now_add=True) end_time = models.TimeField(blank=True, null=True) person = models.ForeignKey(Person) class GameSession(models.Model): game_type = models.CharField(max_length=2) score = models.PositiveIntegerField(default=0, blank=True) session = models.ForeignKey(Session) I want to have a queryset function to return total score of each

order_by on Many-to-Many field results in duplicate entries in queryset

帅比萌擦擦* 提交于 2021-02-07 07:21:59
问题 I am attempting to perform an order_by based a m2m field, but it ends up creating duplicate entries in my queryset. I have been searching through the django documentation and related questions on stack exchange, but I haven't been able to come up with any solutions. Models: class WorkOrder(models.Model): ... appointment = models.ManyToManyField(Appointment, null=True, blank=True, related_name = 'appointment_from_schedule') ... class Appointment(models.Model): title = models.CharField(max

How to convert a list in to queryset django

[亡魂溺海] 提交于 2021-02-06 02:27:33
问题 I have a queryset(which is filtered actually) as below posts = [<Post: published>, <Post: needs>, <Post: be>, <Post: issues>, <Post: to>, <Post: tags>] But i need to filter the above queryset manually with the some field coming from another table/place etc. so i had filtered something like below custom_list = [] for rec in posts: if 'string_or_field' in rec.tags.all(): custom_list.extend(rec) or custom_list = [rec for rec in posts if 'string_or_field' in rec.tags.all()] So as we can observe

How to use Django QuerySet.union() in ModelAdmin.formfield_for_manytomany()?

て烟熏妆下的殇ゞ 提交于 2021-02-05 06:10:26
问题 Not sure what I am doing wrong here: I tried to use QuerySet.union(), in Django 2.2.10, to combine two querysets (for the same model) inside ModelAdmin.formfield_for_manytomany() . However, when the form is saved, the entire queryset is selected, regardless of the actual selection made. Please consider the minimal example below, based on the standard Django Article/Publication example. from django.db import models from django.contrib import admin class Publication(models.Model): pass class