django-orm

Django filter on TimeField

家住魔仙堡 提交于 2019-12-12 02:56:22
问题 In my model I have a TimeField: time = models.TimeField(db_index=True) Now I have to filter by this value: for example I need to make something like: time == "12:30" **** EDIT ***** I need also to make something like: time is in a range : from 12:15 to 12:15 How can I make it? 回答1: You can do something like this: YourModel.objects.filter(time__contains='16:23') If you use this instead: YourModel.objects.filter(time='16:23') You will never find it since TimeField objects are save including

Complex SUM from multiple tables

北战南征 提交于 2019-12-12 01:27:36
问题 Here are my tables: CREATE TABLE component (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT UNIQUE); CREATE TABLE file (id INTEGER PRIMARY KEY AUTOINCREMENT, component_id INTEGER, name TEXT UNIQUE); CREATE TABLE function (id INTEGER PRIMARY KEY AUTOINCREMENT, file_id INTEGER, name TEXT, FOREIGN KEY(file_id) REFERENCES file(id), UNIQUE(file_id, name)); CREATE TABLE version (id INTEGER PRIMARY KEY AUTOINCREMENT, version TEXT UNIQUE); CREATE TABLE data (id INTEGER PRIMARY KEY AUTOINCREMENT, file

Update ManyToManyField in Django from form data

懵懂的女人 提交于 2019-12-11 21:42:08
问题 I am passing form like this: guest = get_object_or_404(Guest, id=guest_id) data = {'full_name': guest.full_name, 'street_address': guest.street_address, 'city': guest.city, 'state': guest.state, 'zip_code': guest.zip_code, 'guests': guest.guests, 'children': guest.children,'email': guest.email, 'phone_number': guest.phone_number, 'gift_description': guest.gift_description, 'status': guest.status.all()} form = GuestForm(initial=data) Getting form data via post like this: form = GuestForm

Django subquery in insert

懵懂的女人 提交于 2019-12-11 19:18:16
问题 Is it possible to force django to make a subquery when I try to insert some values? This produces two separate queries: CommunicationLog.objects.create( device='asdfasdf', date_request=CommunicationLog.objects.get(pk=343).date_request, content_obj_id=338, type_request=1, type_change=2 ) 回答1: You definitely cannot do it by using create . There's no available API that will let you do it, since this is very unusual use case. You have to fall back to raw sql. 回答2: Even if the API won't let you do

Django: how to execute code ONLY after the first time a M2M relationship is added?

一个人想着一个人 提交于 2019-12-11 19:00:16
问题 I'm trying to get create_reminder_send_message() executed THE FIRST TIME the Reminder object is saved AND the Reminder.users is saved. The code as it is executes every time I update the object... what am I missing? How can I accomplish what I want? class Reminder(models.Model): METHODS = ( ('EM', 'Send Email'), ('TS', 'Create Dashboard Task'), ('ET', 'Both (recommended)') ) info = models.TextField() method = models.CharField(max_length=3, choices=METHODS, db_index=True, help_text='''How

Get the records between two dates

情到浓时终转凉″ 提交于 2019-12-11 15:43:26
问题 I have a table called Billing which contains the below columns id| resource_name | start_date | end_date | total_amount This table is populated with data for different resource with different start_date and end_dates. I need to retrieve the data from the tables. Sample Record id| resource_name | start_date | end_date | total_amount 1 | abc | 2018-09-15 03:00:00 | 2018-09-15 04:00:00 | 20 If I write a query like this it will return the above result select * from billing where start_date >=

Django paginator not responsive on last page

∥☆過路亽.° 提交于 2019-12-11 15:39:08
问题 Iterating over a large QuerySet doesn't seem to be a viable option in Django 2.0 anymore. I've tried to speed it up using Django's own Paginator. def read_from_db_4(): paginator = Paginator(DataSet.objects.filter(status_id=1).order_by('id'), 1000) l = [] print("{} iterations!".format(paginator.num_pages)) for page in range(1, paginator.num_pages+1): l = l + list(paginator.page(page).object_list) print("{}, next page...".format(page)) return l This little function is reasonably quick but will

Django ORM LEFT JOIN on fields with same values

倾然丶 夕夏残阳落幕 提交于 2019-12-11 15:29:47
问题 I am writing web-interface for hydrologists. Hydrologist should see table with different hydrological measurements like this. +----------------+----------------------+-------+--------------------+-------------+------------------+ | observation_id | observation_datetime | level | water_temperature |precipitation|precipitation_type| +----------------+----------------------+-------+--------------------+-------------+------------------+ | 1 | 2019-03-11 11:00:00 | 11 | 21 | 31 | | 2 | 2019-03-12

Django ORM's contains vs SQL's LIKE

情到浓时终转凉″ 提交于 2019-12-11 15:27:52
问题 I'm working on Django 2.1.5 and MySQL 14.14 . I'm trying to query using a wildcard search as below for single character replacement. 98765?321 - returns all the numbers in the database column with a single replacement for ? - at most 10 numbers will be returned. 98?65?321 - returns all the numbers with single replacement for both ? s - at most 100 numbers will be returned. 98?65?32? - returns all the numbers with single replacement for first ? and all the numbers ending with that for second ?

How to aggregate the average of a calculation based on two columns?

梦想的初衷 提交于 2019-12-11 13:48:58
问题 I want to write a Django query to give me the average across all rows in my table. My model looks like class StatByDow(models.Model): total_score = models.DecimalField(default=0, max_digits=12, decimal_places=2) num_articles = models.IntegerField(default=0) day_of_week = IntegerField( null=True, validators=[ MaxValueValidator(6), MinValueValidator(0) ] ) and I attempt to calculate the average like this everything_avg = StatByDow.objects.all().aggregate(Avg(Func(F('total_score') / F('num