django-queryset

Is there a way to augment django QuerySets with extra attributes?

﹥>﹥吖頭↗ 提交于 2019-12-19 10:01:56
问题 I'm trying to add some extra attributes to the elements of a QuerySet so I can use the extra information in templates, instead of hitting the database multiple times. Let me illustrate by example, assuming we have Books with ForeignKeys to Authors. >>> books = Book.objects.filter(author__id=1) >>> for book in books: ... book.price = 2 # "price" is not defined in the Book model >>> # Check I can get back the extra information (this works in templates too): >>> books[0].price 2 >>> # but it's

Django: OR queries with dynamic field names

空扰寡人 提交于 2019-12-19 09:45:47
问题 I have a value and want to get all instances having the value in one or more column. And to make this a bit more complex, the field list is dynamic. So, what I have is: ['field1', 'field2', 'field3', ...] What I need is: Q(field1='value') | Q(field2='value') | Q(field3='value') | ... How can I get this? 回答1: Use ** dictionary-to-kw-args expansion: q = Q() for field in fields: q = q | Q(**{field: "value"}) (as Q() yield a Q which "does nothing", as far as I can tell) 来源: https://stackoverflow

Django reverse to contains/icontains

隐身守侯 提交于 2019-12-18 19:45:52
问题 In this question was solved problem for reverse LIKE operation in SQL, for example if field name is "Peter Johnson", we could find it by such query: select name from user where "Mr. Peter Johnson" like CONCAT('%', name, '%') Is there any way to do such thing in Django Q object (I'm building a big query, so using raw SQL query will not be rational)? 回答1: While extras do provide an expanded complex functionality for edge cases, extras should be treated as last resort and likely going to be

Django: Total birthdays each day for the next 30 days

扶醉桌前 提交于 2019-12-18 18:28:11
问题 I've got a model similar to this: class Person(models.Model): name = models.CharField(max_length=40) birthday = DateTimeField() # their next birthday I would like to get a list of the total birthdays for each day for the next 30 days. So for example, the list would look like this: [[9, 0], [10, 3], [11, 1], [12, 1], [13, 5], ... #30 entries in list Each list entry in the list is a date number followed by the number of birthdays on that day. So for example on the 9th of May there are 0

django filter icontains match whole words only

半腔热情 提交于 2019-12-18 16:45:24
问题 I am using the filter icontains to search for words but I only want it to match whole words. e.g. if I searched for liver I wouldn't want it returning delivery. my query looks like this MyModel.objects.filter(title__icontains=search_word) I have seen the filter __search but this does not bring back results with 3 characters or less and the site I am building contains a lot of these which could be searched for, e.g. 'bbc' I do not have access to the db but if anyone knows how I can disable

get foreign key objects in a single query - Django

谁说胖子不能爱 提交于 2019-12-18 14:57:08
问题 I have 2 models in my django code: class ModelA(models.Model): name = models.CharField(max_length=255) description = models.CharField(max_length=255) created_by = models.ForeignKey(User) class ModelB(models.Model): category = models.CharField(max_length=255) modela_link = models.ForeignKey(ModelA, 'modelb_link') functions = models.CharField(max_length=255) created_by = models.ForeignKey(User) Say ModelA has 100 records, all of which may or may not have links to ModelB Now say I want to get a

Django object multiple exclude()

Deadly 提交于 2019-12-18 14:27:54
问题 Is there a way to do a query and exclude a list of things, instead of calling exclude multiple times? 回答1: Based on your reply to Ned, it sounds like you just want to exclude a list of tags. So you could just use the in filter: names_to_exclude = [o.name for o in objects_to_exclude] Foo.objects.exclude(name__in=names_to_exclude) Does that do what you want? 回答2: What's wrong with calling exclude multiple times? Queries are lazy, nothing happens until you try to pull data from it, so there's no

Django object multiple exclude()

限于喜欢 提交于 2019-12-18 14:27:00
问题 Is there a way to do a query and exclude a list of things, instead of calling exclude multiple times? 回答1: Based on your reply to Ned, it sounds like you just want to exclude a list of tags. So you could just use the in filter: names_to_exclude = [o.name for o in objects_to_exclude] Foo.objects.exclude(name__in=names_to_exclude) Does that do what you want? 回答2: What's wrong with calling exclude multiple times? Queries are lazy, nothing happens until you try to pull data from it, so there's no

Django object multiple exclude()

六月ゝ 毕业季﹏ 提交于 2019-12-18 14:26:47
问题 Is there a way to do a query and exclude a list of things, instead of calling exclude multiple times? 回答1: Based on your reply to Ned, it sounds like you just want to exclude a list of tags. So you could just use the in filter: names_to_exclude = [o.name for o in objects_to_exclude] Foo.objects.exclude(name__in=names_to_exclude) Does that do what you want? 回答2: What's wrong with calling exclude multiple times? Queries are lazy, nothing happens until you try to pull data from it, so there's no

Django MySQL distinct query for getting multiple values

放肆的年华 提交于 2019-12-18 13:10:06
问题 I have a MySQL database unfortunately used with Django 1.4.1. Distinct function is only working for POSTGRESQL if i get it right. I have to make a distinct query consist of multiple values while only distinct one, Like; This one works for POSTGRE but not with MYSQL, I get the following error; DISTINCT ON fields is not supported by this database backend staff = Staff.objects.order_by('person__full_name').distinct('person__full_name') Then I tried staff = Staff.objects.values('person__full_name