django-orm

In Django ORM, “values” and “annotate” are not working to group by

主宰稳场 提交于 2019-12-12 07:54:55
问题 I have a table like this: Now I want to sum up the meals on each date. I have written the code below. But it doesn't work as I wanted. Model: class Meal(models.Model): user = models.ForeignKey(settings.AUTH_USER_MODEL, default=1) date_of_meal = models.DateField() morning_meal = models.SmallIntegerField(default=0) mid_day_meal = models.SmallIntegerField(default=0) night_meal = models.SmallIntegerField(default=0) updated = models.DateTimeField(auto_now=True, auto_now_add=False) timestamp =

How to get latest unique entries from sqlite db with the counter of entries via Django ORM

强颜欢笑 提交于 2019-12-12 07:01:53
问题 I have a SQLite db which looks like this: |ID|DateTime|Lang|Details| |1 |16 Oct | GB | GB1 | |2 |15 Oct | GB | GB2 | |3 |17 Oct | ES | ES1 | |4 |13 Oct | ES | ES2 | |5 |15 Oct | ES | ES3 | |6 |10 Oct | CH | CH1 | I need a Django query to select this: |1 |16 Oct | GB | GB1 | 2 | |3 |17 Oct | ES | ES1 | 3 | |6 |10 Oct | CH | CH1 | 1 | So this is unique (by Lang) latest (by DateTime) entries with the number of occurrences (by Lang). Is it possible to do this with a single SQL or Django-ORM query

LIKE query in Django ORM

╄→尐↘猪︶ㄣ 提交于 2019-12-12 06:39:13
问题 How can I make query, with rule: order by words, that contain founded string as close as possible to the beginning. Is it real? For example: search 'hi' result: hi all hi cat Peter, hi I come to you and say hi query = query & Q(name__contains=params['name']) 回答1: SearchRank is the answer to your question, hope you are using django with postgreSQL, I will not explain as it is well explained in the django docs. Here is the link to it:- Django SearchRank Enjoy and rejoice :-) 来源: https:/

filter/limit django order_by

无人久伴 提交于 2019-12-12 06:06:22
问题 Using the django ORM, Is there a way to limit or filter the scope of what order_by applies to? Product.objects.filter( product_type__name=choices.PRODUCT_BOOK ).order_by( # need to order these last names by those # who have a role code of "A01", then "B01" 'contributor__writer__last_name' ) 回答1: Even using raw SQL is not possible to apply an order by just to some rows. So the best way, order alls and group by role_code. Also you can get contributors with role_code "A01" or "B01" , order them,

filtering the order_by relationship in Django ORM

本秂侑毒 提交于 2019-12-12 06:03:03
问题 In the below, product has many writers through contributor, and contributor.role_code defines the exact kind of contribution made to the product. Is it possible with the Django ORM to filter the contributors referenced by the order_by() method below? E.g. I want to order products only by contributors such that contributor.role_code in ['A01', 'B01'] . Product.objects.filter( product_type__name=choices.PRODUCT_BOOK ).order_by( 'contributor__writer__last_name' # filter which contributors it

Django performance with `not in` filter with giant list?

牧云@^-^@ 提交于 2019-12-12 04:58:13
问题 I am currently making a school project where I am building a website that lets you rate nearby restaurants (1-5 stars). The only caveat is that I will show users the restaurants based on location (e.g. closest ones first) and that once they have rated a restaurant, it will never be shown to them again. Currently I have something like this in mind class User(models.Model): username = models.CharField(primary_key=True, max_length=20) location = models.SomeGeoPointField ... class Restaurant

When creating a model instance how to fill ManyToMany field?

喜夏-厌秋 提交于 2019-12-12 04:53:46
问题 I want to create model instance like this: new_tweet = Tweet.objects.create(text = tweet_object.text, date = tweet_object.date, username = tweet_object.username, retweet = tweet_object.retweet.all(), is_ret = True) It's all going well until this: retweet = tweet_object.retweet.all() . It returns this error: 'retweet' is an invalid keyword argument for this function This is a ManyToMany field. So how to fill this field when creating new model instance? By the way tweet_object.retweet.all() is

Django - multiple DB + multiple models

主宰稳场 提交于 2019-12-12 04:19:44
问题 I have two databases and two models:one the admin and the is user. I want to sync my models to the two databases; admin model to database A and user model to database B; so my problem the how to sync the two models to two databases? User should in default DB and admin in admin model. Here's what I've tried:- class User(models.Model): job_id = models.CharField(max_length = 255) time = models.DateTimeField( auto_now_add = True, db_index = True) class Meta: app_label = 'user_data' class Admin

Convert OneToOneField to MultipleTableInheritance

孤人 提交于 2019-12-12 03:59:23
问题 Build of this question: Which is better: Foreign Keys or Model Inheritance? I would like to know if it is possible to replace a OneToOne field by MTI? A.k. I have: class GroupUser(models.Model): group = models.OneToOneField(Group) ...other fields.... and I want: class GroupUser(Group): ...other fields.... I think that should be faster, or not? Is it possible? 回答1: It won't be faster, because your parent class object will still have a field in the database that links to the child class if you

django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet

梦想与她 提交于 2019-12-12 03:18:55
问题 In Django 1.7 this code caused errors in django.setup() : class MyModel(models.Model): special_foo=Foo.objects.filter(name__contains='special') In my case there I got this: django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet. But I have seen recursion errors in django.setup() trying to run django.setup() again, too. 回答1: I solved this with properties at class level. class MyModel(models.Model): @classproperty def special_foo(cls): return Foo.objects.filter(name__contains=