django-queryset

Django query to join records of two tables

北城以北 提交于 2019-12-24 17:08:20
问题 I am using django-mssql 1.6.2 package with django 1.7 to get one or several records of tables from sql server 2008. When I call "get" or "filter" as follows, everything is fine but my server program is very slow. Consider the following tables: class Contact(models.Model): id = models.IntegerField(primary_key=True, unique=True, null=False) address = models.CharField(max_length = 100) phone = models.IntegerField(unique=True) class Parent(models.Model): id = models.IntegerField(primary_key=True,

cross instance calculations in django queryset

拜拜、爱过 提交于 2019-12-24 10:55:45
问题 Not sure that it's (a) doable and (b) if I formulate the task correctly. Perhaps the right way is refactoring the db design, but I would appreciate any opinion on that. I have a model in django app, where I track the times a user enters and exits a certain page (via either form submission or just closing the broswer window). I do tracking using django channels, but it does not matter in this case. The model looks like: class TimeStamp(models.Model): class Meta: get_latest_by = 'enter_time'

query an object using uuid in django

蹲街弑〆低调 提交于 2019-12-24 10:09:09
问题 I am using uuid for creating an id field which is primary key as below import uuid class User_Profile(models.Model): id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) So whenever we save an object into the database it was saving as an UUID instance instead of string as below user_profiles = User_Profile.objects.values_list('id', flat=True) print user_profiles [UUID('193b6acc-2b78-4ddc-9ef8-632cde33ef74')] now how to query it by using django ORM ? since it was not

Django - QuerySet - Get count for all determined child objects inside all parent objects

感情迁移 提交于 2019-12-24 08:11:46
问题 I've got this model: class Order(models.Model): #bunch of fields# class Laptop(models.Model): #bunch of fields# class Camera(models.Model): #bunch of fields# class MusicPlayer(models.Model): #bunch of fields# The last three have a foreign key associated to the Order class. I need to retrieve via a QuerySet the summed count for each child object for all orders, grouped by each one of them. The result should look something like this: Laptop:5 Camera:10 MusicPlayer:1 I've tried using different

Django weighted query (annotated values)

て烟熏妆下的殇ゞ 提交于 2019-12-24 07:09:35
问题 I am attempting to created a query and sort it based on a custom calculation of weights. I require some help as the solution I have come to does indeed work but the performance is not where I'd like it to be What I have is a Media object. It has related Comments, Likes and Orders. What currently works but is a complete hacky mess is the following query: products = Media.objects \ .select_related( 'image', 'currency', 'user', 'user__image', ) \ .prefetch_related('category', 'tags') \ .exclude

Django Admin: Custom ordering according to concatenated charfields of a related manytomany model

狂风中的少年 提交于 2019-12-24 03:43:34
问题 Here are my simplified models: class MasterFood(models.Model): class Meta: verbose_name_plural = 'Master Foods' FOOD_TYPE_CHOICES = ( ('F', 'Fats'), ('C', 'Carbohydrates'), ('P', 'Proteins'), ('O', 'Others'), ) food_name = models.CharField(max_length=255) food_type = models.CharField(max_length=20,choices=FOOD_TYPE_CHOICES) def __str__(self): return self.food_name class MasterMeal(models.Model): class Meta: verbose_name_plural = 'Master Meal Plan Meals' proteins = models.ManyToManyField(to=

Check is_null for list of field in Django queryset filtering

 ̄綄美尐妖づ 提交于 2019-12-24 03:43:27
问题 I have create model with this fields: field_1 , field_2, field_3, ... , field_n And I want to filter all objects that these k fields: field_1 , field_2, .... , field_k of those objects is n't Null . My wrong answer: I did think that I must create this list of fields that I want to set in my query: my_list = [`field_1` , `field_2`, .... , `field_k`] and create my query like this: my_objects = Class.objects.filter([ (eval(field_name +"__isnull = False") ) for field_name in my_list ]) But it was

Django: Best way to retrieve object

别等时光非礼了梦想. 提交于 2019-12-24 03:12:32
问题 Lets assume that I have a class Person with a field email . I want to get a single record based on queryset and assign it to variable person . Sadly I cannot do person = Person.objects.filter(email="xxx@xxx.xxx")[0] because I might get an exception. So: Scenario 1 : Include the above code in a try-except. Scenario 2 (which I'm currently using): person_arr = Person.objects.filter(email="xxx@xxx.xxx") if person_arr.exists(): person = person_arr[0] .... Scenario 3 for person in Person.objects

Django Using order_by with .annotate() and getting related field

╄→尐↘猪︶ㄣ 提交于 2019-12-24 03:07:09
问题 I have the following data, This query groups by topicid, and then in each group gets the max date, frequency of posts and counts the number of authors as contributors, info_model = InfoModel.objects.values('topicid') .annotate( max=Max('date'), freq=Count('postid'), contributors=Count('author', distinct=True)) This query can then be displayed as follows, Q.1 (SOLVED) How can I order the rows by date, from most recent down? I did appended .order_by('date') to the query, which seems like the

Django query ForeignKey Count() zero

蓝咒 提交于 2019-12-24 00:09:54
问题 I have 3 tables: Truck with the fields: id , name .... Menu with the fields: id , itemname , id_foodtype , id_truck ... Foodtype with the fields: id , type ... I want to get a summary like: id name total 10 Alcoholic drink 0 5 Appetizer 11 My problem is to return the results with 0 elements . I tried an SQL query like this: SELECT ft.id, ft.name, COUNT(me.id) total FROM foodtype ft LEFT JOIN menu me ON ft.id = me.id_foodtype LEFT JOIN truck tr ON tr.id = me.id_truck AND tr.id = 3 GROUP BY ft