django-queryset

Can i sort query set return objects in order to function result

半城伤御伤魂 提交于 2020-01-02 08:53:34
问题 I am writing a web based music application and I want implement some feature that user can see most favor album in last week-month-year. so this is my model : class album(models.Model): def get_weely_count(): ... def get_monthly_count(): ... def get_yearly_count(): ... class like(models.Model): created_at = models.DateField() albumID = models.ForeignKey(Album) Now I want to receive albums that most liked in last week or last month or last year,I want done some thing like this(but I can not):

Can i sort query set return objects in order to function result

落花浮王杯 提交于 2020-01-02 08:53:31
问题 I am writing a web based music application and I want implement some feature that user can see most favor album in last week-month-year. so this is my model : class album(models.Model): def get_weely_count(): ... def get_monthly_count(): ... def get_yearly_count(): ... class like(models.Model): created_at = models.DateField() albumID = models.ForeignKey(Album) Now I want to receive albums that most liked in last week or last month or last year,I want done some thing like this(but I can not):

how to retrive values form RawQuerySet in django?

有些话、适合烂在心里 提交于 2020-01-02 07:52:09
问题 my input query is query = "select * from tab1 left join tab2 on tab2.patient_id =tab1.patient_id ,tab3 left join tab4 on tab4.patient_id =tab3.patient_id" data = model_name.objects.raw(query) How do you retrieve values from a RawQuerySet ? 回答1: The result obtained by making raw queries using raw method of Manager generates instances similar to instances generated using get or filter method. To get a field simply do obj_name.attr . For eg: class Tab(models.Model): field1 = models.BooleanField(

How does one find the entities in a Django query set that are not in another specified query set?

瘦欲@ 提交于 2020-01-02 06:40:05
问题 I am using Django to develop a course registration site for an educational institution. Suppose I have two Django query sets, one that comprises of courses that occupy session 1 (set A) and one that comprises of courses in session 2 (set B): A = session1.courses.all() B = session2.courses.all() There is much overlap between these two query sets. What is an efficient way to obtain the set of courses within set B, but not in set A? I believe this is equivalent to taking out the intersection of

Django: Filtering datetime field by *only* the year value?

℡╲_俬逩灬. 提交于 2020-01-01 08:32:49
问题 I'm trying to spit out a django page which lists all entries by the year they were created. So, for example: 2010: Note 4 Note 5 Note 6 2009: Note 1 Note 2 Note 3 It's proving more difficult than I would have expected. The model from which the data comes is below: class Note(models.Model): business = models.ForeignKey(Business) note = models.TextField() created = models.DateTimeField(auto_now_add=True) updated = models.DateTimeField(auto_now=True) class Meta: db_table = 'client_note'

Django Admin: Getting a QuerySet filtered according to GET string, exactly as seen in the change list?

南笙酒味 提交于 2020-01-01 05:52:05
问题 In the Django admin, the user can set filters which limit the rows displayed in the change list. How can I get a QuerySet instance with filters set as defined by the query string? For instance, if I pass ?start_date_gte=2009-11-06, the Django admin will apply a qs.filter(start_date__gte...) somewhere. How can I access such a QuerySet? I need this since obviously I don't want to rewrite the code that takes these GET parameters and filter()s a QuerySet accordingly. 回答1: Looks interesting.

sort django queryset by latest instance of a subset of related model

安稳与你 提交于 2019-12-31 02:59:28
问题 I have an Order model and order_event model. Each order_event has a foreignkey to order. so from an order instance i can get: myorder.order_event_set . I want to get a list of all orders but i want them to be sorted by the date of the last event. A statement like this works to sort by the latest event date: queryset = Order.objects.all().annotate( latest_event_date=Max('order_event__event_datetime') ).order_by('latest_event_date') However, what I really need is a list of all orders sorted by

How can I get computed elements of a table in a django queryset?

依然范特西╮ 提交于 2019-12-30 17:27:21
问题 I'm trying to use django's queryset API to emulate the following query: SELECT EXTRACT(year FROM chosen_date) AS year, EXTRACT(month FROM chosen_date) AS month, date_paid IS NOT NULL as is_paid FROM (SELECT (CASE WHEN date_due IS NULL THEN date_due ELSE date END) AS chosen_date,* FROM invoice_invoice) as t1; The idea is mainly that in certain situations, I'd rather use the date_due column rather than the date column in some situations, but that , since date_due is optional, I sometimes have

Django: union of different queryset on the same model

て烟熏妆下的殇ゞ 提交于 2019-12-30 11:18:51
问题 I'm programming a search on a model and I have a problem. My model is almost like: class Serials(models.Model): id = models.AutoField(primary_key=True) code = models.CharField("Code", max_length=50) name = models.CharField("Name", max_length=2000) and I have in the database tuples like these: 1 BOSTON The new Boston 2 NYT New York journal 3 NEWTON The old journal of Mass 4 ANEWVIEW The view of the young people If I search for the string new , what I want to have is: first the names that start

Queryset sorting: Specifying column collation for django ORM query

∥☆過路亽.° 提交于 2019-12-30 07:10:08
问题 I started investigating why my Django Model.objects.filter(condition = variable).order_by(textcolumn) queries do not yield objects in correct order. And found out that it is database (Postgresql) issue. In my earlier question (Postgresql sorting language specific characters (collation)) i figured out (with a lot of help from zero323 in actually getting it to work) that i can specify collation per database query like this: SELECT nimi COLLATE "et_EE" FROM test ORDER BY nimi ASC; But as much as