django-queryset

Django queryset - Isn’t it possible to filter on a FloatField?

痴心易碎 提交于 2019-12-23 01:47:06
问题 I’m trying to do a very simple operation but I get an issue. I have a simple model : class MyModel(models.Model): date = models.DateTimeField(null=False) value = models.FloatField(null=True) interval = models.IntegerField(null=True) My goal is to get the MyModel object with the biggest value. #Get the value value = MyModel.objects.filter(date__gte=’2014-05-01’, date__lte=’2014-05-31’).aggregate(Max(‘value’))[‘value__max’] # Get the object corresponding to the max value my_object = MyModel

django combine queries on two different base models

99封情书 提交于 2019-12-23 00:26:33
问题 I have two different different queryset, I want to make union of both queryset q1 = tbl_nt_123.objects.values_list('id', 'value', 'geometry').filter(restriction='height')\ .exclude(condition_id__in=tbl_conditions.objects.filter(version_id=5).values_list('condition_id',flat=True)) q2 = tbl_network.objects.all().filter(Q(name="height"), Q(state_id=1) | Q(state_id=2)).values_list('id', 'value', 'geometry'); I have tried this function queryset = (q1) | (q2) facing this error ' django combine

django combine queries on two different base models

六月ゝ 毕业季﹏ 提交于 2019-12-23 00:26:17
问题 I have two different different queryset, I want to make union of both queryset q1 = tbl_nt_123.objects.values_list('id', 'value', 'geometry').filter(restriction='height')\ .exclude(condition_id__in=tbl_conditions.objects.filter(version_id=5).values_list('condition_id',flat=True)) q2 = tbl_network.objects.all().filter(Q(name="height"), Q(state_id=1) | Q(state_id=2)).values_list('id', 'value', 'geometry'); I have tried this function queryset = (q1) | (q2) facing this error ' django combine

Django model inheritance - only want instances of parent class in a query

我与影子孤独终老i 提交于 2019-12-22 20:42:23
问题 Let's say I have 2 models, one being the parent of another. How can I query all Places that aren't restaurants in Django? Place.objects.all() would include all restaurants right? I want to exclude the children from the results. Thank you! class Place(models.Model): name = models.CharField(max_length=50) address = models.CharField(max_length=80) class Restaurant(Place): serves_hot_dogs = models.BooleanField() serves_pizza = models.BooleanField() 回答1: According to the documentation, you can

How to increment the year on a datetimefield in Django with update()?

六眼飞鱼酱① 提交于 2019-12-22 17:49:36
问题 Is there a way to increment the year on filtered objects using the update() method? I am using: python 2.6.5 django 1.2.1 final mysql Ver 14.14 Distrib 5.1.41 I know it's possible to do something like this: today = datetime.datetime.today() for event in Event.objects.filter(end_date__lt=today).iterator(): event.start_date = festival.start_date + datetime.timedelta(365) event.end_date = festival.end_date + datetime.timedelta(365) event.save() However, in some cases, I would prefer to use the

Changes in DB data not reflecting in the Django queryset in a continuously looping script

你离开我真会死。 提交于 2019-12-22 12:56:11
问题 I am using Django's ORM to get newly added entries from a Db and pass them to a Messaging queue. I am doing this in an infinite while loop , The PROBLEM in every loop iteration I am getting the same queryset even when I have added/deleted/edited entries when this script is running, The code goes like this : while True : sleep(10 seconds) # Below is the problem line, I get the same query-set every time in new_objects # even when I have added/deleted/edited entries while this daemon is running.

Django reverse lookup chaining queryset

▼魔方 西西 提交于 2019-12-22 11:28:26
问题 I have several models that I need to do reverse lookups in, then somehow chain the querysets together. I got 3 models, (1) designers create (2) projects, and projects contain (3) uploaded images. Models class Designer(models.Model): ... class Project(models.Model): designer = models.ForeignKey('Designer') class UploadedImage(models.Model): project = models.ForeignKey('Project') So a designer opens up his page and wants to see all his projects and some images associated with his projects, I

Django - Filter queryset with child objects (ForeignKey)

北慕城南 提交于 2019-12-22 10:02:24
问题 I have 3 Models, and 2 of them correspond to the first one. class Parent(models.Model): name = models.CharField.... ... class Child1(models.Model): parent = models.ForeignKey(Parent) ... class Child2(models.Model): parent = models.ForeignKey(Parent) ... Now, in my view I have 2 querysets filtered of Child1 and Child2 objects. Is there a way to retrieve all the Parent objects that are in the filtered querysets? Something like... children1 = Child1.objects.filter(blah=blah) children2 = Child2

Django Query extremely slow

只愿长相守 提交于 2019-12-22 06:44:53
问题 i got a problem with an Django application. Queries on the model Scope are extremly slow and after some debugging i still got no clue where the problem is. When i query the db like scope = Scope.objects.get(pk='Esoterik I') in django it takes 5 to 10 seconds. The database has less than 10 entries and an index on the primary key. so it is way too slow. When executing the an equivalent query on the db like SELECT * FROM scope WHERE title='Esoterik I'; everything is ok, it takes only about 50ms.

Django Select query with specific column

风格不统一 提交于 2019-12-22 06:35:56
问题 I have a model called MessagePerson which has the fields of details which depict the messages for a particular person, who's details are in Personal models. I even have separate model which has a foreign key reference to Personal class. class Personal(models.Model): name = models.CharField(max_length=20,primary_key=True) email = models.EmailField(blank=True,null=True) address = models.CharField(max_length=50,blank=True,null=True) contact = models.CharField(max_length=20) pic = models