django-queryset

Django: Why is Foo.objects.extra(…) So Much Faster Than Foo.objects.raw?

匆匆过客 提交于 2021-02-19 01:59:28
问题 So I am trying to optimize a fairly odd query, but this is a legacy database so I make do with what I have. These are the queries I am trying. They provide the same output at this point. w is my queryset. def future_schedule(request): past = datetime.date.today()-datetime.timedelta(days=730) extra_select = { 'addlcomplete': 'SELECT Complete FROM tblAdditionalDates WHERE Checkin.ShortSampleID = tblAdditionalDates.ShortSampleID', 'addldate': 'SELECT AddlDate FROM tblAdditionalDates WHERE

Django nested QuerySets

ぐ巨炮叔叔 提交于 2021-02-18 07:32:00
问题 I have a Django data model like this (data fields omitted): class Atom(Model): pass class State(Model): atom = ForeignKey(Atom) class Transition(Model): atom = ForeignKey(Atom) upstate = ForeignKey(State,related_name='uptrans') lostate = ForeignKey(State,related_name='lotrans') When I query, the fields to be restricted can be in either model, so it is easiest to query on Transition.objects.filter(...) since all fields in the other models can be reached through the foreign keys. Let's call the

Django nested QuerySets

回眸只為那壹抹淺笑 提交于 2021-02-18 07:31:16
问题 I have a Django data model like this (data fields omitted): class Atom(Model): pass class State(Model): atom = ForeignKey(Atom) class Transition(Model): atom = ForeignKey(Atom) upstate = ForeignKey(State,related_name='uptrans') lostate = ForeignKey(State,related_name='lotrans') When I query, the fields to be restricted can be in either model, so it is easiest to query on Transition.objects.filter(...) since all fields in the other models can be reached through the foreign keys. Let's call the

Django: Duplicated logic between properties and queryset annotations

只愿长相守 提交于 2021-02-15 14:56:25
问题 When I want to define my business logic, I'm struggling finding the right way to do this, because I often both need a property AND a custom queryset to get the same info. In the end, the logic is duplicated. Let me explain... First, after defining my class, I naturally start writing a simple property for data I need: class PickupTimeSlot(models.Model): @property def nb_bookings(self) -> int: """ How many times this time slot is booked? """ return self.order_set.validated().count() Then, I

Django Aggregate Query Include Zero Count

末鹿安然 提交于 2021-02-11 16:11:27
问题 In my Django application, I'm trying to get a Count of all Student submitted Paper s, including students who have submitted NO papers (represented as count=0). models.py class Student(models.Model): idstudent = models.AutoField(primary_key=True) student_name = models.CharField(max_length=250, null=False, blank=False, verbose_name='Student Name') class Paper(models.Model): idpaper = models.AutoField(primary_key=True) student = models.ForeignKey(Student, on_delete=models.PROTECT, null=False,

Ordering a Django queryset based on other list with ids and scores

旧时模样 提交于 2021-02-10 17:49:54
问题 I'm a bit mentally stuck at something, that seems really simple at first glance. I'm grabbing a list of ids to be selected and scores to sort them based on. My current solution is the following: ids = [1, 2, 3, 4, 5] items = Item.objects.filter(pk__in=ids) Now I need to add a score based ordering somehow so I'll build the following list: scores = [ {'id': 1, 'score': 15}, {'id': 2, 'score': 7}, {'id': 3, 'score': 17}, {'id': 4, 'score': 11}, {'id': 5, 'score': 9}, ] ids = [score['id'] for

Ordering a Django queryset based on other list with ids and scores

最后都变了- 提交于 2021-02-10 17:49:21
问题 I'm a bit mentally stuck at something, that seems really simple at first glance. I'm grabbing a list of ids to be selected and scores to sort them based on. My current solution is the following: ids = [1, 2, 3, 4, 5] items = Item.objects.filter(pk__in=ids) Now I need to add a score based ordering somehow so I'll build the following list: scores = [ {'id': 1, 'score': 15}, {'id': 2, 'score': 7}, {'id': 3, 'score': 17}, {'id': 4, 'score': 11}, {'id': 5, 'score': 9}, ] ids = [score['id'] for

How can I get the ids of a queryset from a model and store them into a different model using a form?

无人久伴 提交于 2021-02-08 09:38:06
问题 I have a model called 'Competicion', with some objects and another model called 'Participante'. This second model has two fields: a foreignkey with the user and another foreingkey to 'Competicion'. In the view, I've made queryset from 'Competicion' and with a for loop in the template I've given each object the button of the form. With storing the user of the current session I have no problem but I want the form to know which object of the queryset it is to grab its id. #I have no problem with

Django queryset with Model method containing another queryset

这一生的挚爱 提交于 2021-02-08 07:17:16
问题 Suppose I have a model, MyModel , with a property method that uses another model's queryset. class OtherModel(models.Model) ... class MyModel(models.Model): simple_attr = models.CharField('Yada yada') @property def complex_attr(self): list_other_model = OtherModel.objects.all() ... # Complex algorithm using queryset from 'OtherModel' and simple_attr return result This causes my get_queryset() method on MyModel to query the database to generate the list_other_model variable every time for

Django: Most efficient way to create a nested dictionary from querying related models?

和自甴很熟 提交于 2021-02-08 04:59:37
问题 In Django, what is the most efficient way to create a nested dictionary of data from querying related and child models? For example, if I have the following models: Parent Children Pets I've seen django's model_to_dict method, and that's pretty cool, so I imagine I could loop through each level's queryset and create a bunch of DB calls on each level, for each instance, but is there a better way? For example, could "prefetch_related" be used to get all three tiers as it is used to get two