django-orm

Django ORM - objects.filter() vs. objects.all().filter() - which one is preferred?

ⅰ亾dé卋堺 提交于 2019-11-29 10:36:53
问题 Very often I see constructs like MyModel.objects.all().filter(...) which will return a QuerySet of the default Mananger. At first all() seems to be quite redundant, because MyMode.objects.filter(...) delivers the same result. However, this seems to be safe for the default Manager only, because of the following two statements in the Django documentation: Excerpt from the Chapter "Adding extra manager methods" A custom Manager method can return anything you want. It doesn’t have to return a

What is the replacement for DateModifierNode in new versions of Django

你。 提交于 2019-11-29 08:04:20
I want to do a query based on two fields of a model, a date, offset by an int, used as a timedelta model.objects.filter(last_date__gte=datetime.now()-timedelta(days=F('interval'))) is a no-go, as a F() expression cannot be passed into a timedelta A little digging, and I discovered DateModifierNode - though it seems it was removed in this commit: https://github.com/django/django/commit/cbb5cdd155668ba771cad6b975676d3b20fed37b (from this now-outdated SO question Django: Using F arguments in datetime.timedelta inside a query ) the commit mentions: The .dates() queries were implemented by using

Django update table using data from another table

最后都变了- 提交于 2019-11-29 06:06:10
问题 I have 2 tables products and catagories connected by foreign key. I need to update field products.new_cost using field catagories.price_markup as following: UPDATE products p INNER JOIN categories c ON p.category_id = c.id SET p.new_cost = ROUND(p.pleer_cost * (1 + c.price_markup/100), -1) WHERE p.update = 1 In SQL it's so easy, but how to do it using Django ORM? My simplified try doesn't work Cannot resolve keyword 'category.price_markup' into field. : Product.actived.select_related(

Django bulk update with string replace

纵然是瞬间 提交于 2019-11-29 02:58:30
问题 I am trying to update and modify a string field Django's ORM. The equivalent SQL to do this is: UPDATE example_table SET string_field = REPLACE(string_field, 'old text', 'new text'); With that query, I expect old text and old text more text to be replaced with new text and new text more text respectively, for all the entries in the string_field column. Bulk update() seems promising, but doesn't allow me to modify only part of the field, and F() expressions only implement numeric changes, not

--fake-initial vs --fake in Django migration?

半腔热情 提交于 2019-11-29 01:12:35
What is the difference between --fake-initial and --fake in Django migrations? What are the dangers of using fake migrations? Anybody knows? Thank you very much to all. I am using django 1.10 e4c5 Well the documentation is very clear about this --fake-initial Allows Django to skip an app’s initial migration if all database tables with the names of all models created by all CreateModel operations in that migration already exist. This option is intended for use when first running migrations against a database that preexisted the use of migrations. This option does not, however, check for

Django ORM, group by day

风流意气都作罢 提交于 2019-11-29 01:04:42
I am trying to group products by DAY, however date_created is a datetime field. Product.objects.values('date_created') \ .annotate(available=Count('available_quantity')) returns: [ {'date_created': datetime.datetime(2012, 4, 14, 13, 3, 6), 'available': 1}, {'date_created': datetime.datetime(2012, 4, 14, 17, 12, 9), 'available': 1}, ... ] I want: [ {'date_created': datetime.datetime(2012, 4, 14), 'available': 2}, ... ] edit: database backend MYSQL San4ez Inspired by this question try this for mysql Product.objects.extra(select={'day': 'date( date_created )'}).values('day') \ .annotate(available

Filtering Many-to-Many relationship by Relationship field in Django

元气小坏坏 提交于 2019-11-29 00:12:16
问题 I'm trying to filter many-to-many relationship by some through Class field. Quoting the Django documentation, i will explain my goal class Person(models.Model): name = models.CharField(max_length=128) def __unicode__(self): return self.name class Group(models.Model): name = models.CharField(max_length=128) members = models.ManyToManyField(Person, through='Membership') def __unicode__(self): return self.name class Membership(models.Model): person = models.ForeignKey(Person) group = models

Django ORM: Group by and Max

不问归期 提交于 2019-11-28 23:23:58
I have a model that looks like this: Requests: user, req_time, req_text In the DB, the records can look like this: id, user_id, req_time, req_text 1 1 TIMESTAMP YES 2 1 TIMESTAMP NO 3 2 TIMESTAMP YES etc. How do I write a Django ORM query that: groups the Requests by user, filters the Requests based on req_text, and also, select the max id of the resulting result set. So for each user, I will return one row which matches the filter condition and also has the greatest id. Botond Béres from django.db.models.aggregates import Max request_values = Requests.objects.filter(req_text='YES') \ .values(

Django: Order_by multiple fields

百般思念 提交于 2019-11-28 23:10:29
I am getting order_by fields in the form of a list. I want to order_by by multiple fields with django orm. List is like below: orderbyList = ['check-in','check-out','location'] I am writing a query is like: modelclassinstance.objects.all().order_by(*orderbyList) Everything i am expecting in a list is dynamic. I don't have predifined set of data.Could some tell me how to write a django ORM with this? Try something like this modelclassinstance.objects.order_by('check-in', 'check-out', 'location') You don't need .all() for this You can also define ordering in your model class something like class

Django equivalent of SQL not in

孤街浪徒 提交于 2019-11-28 22:26:42
I have a very simple query: select * from tbl1 where title not in('asdasd', 'asdasd') . How do I translate that to Django? It's like I want the opposite of: Table.objects.filter(title__in=myListOfTitles) try using exclude Table.objects.exclude(title__in=myListOfTitles) Table.objects.exclude(title__in=myListOfTitles) 来源: https://stackoverflow.com/questions/9003518/django-equivalent-of-sql-not-in