django-orm

django-orm : How to update one-to-one relation field value

非 Y 不嫁゛ 提交于 2019-12-08 03:11:51
问题 models.py class Area(models.Model): area_name = models.CharField(max_length=255, null=False, blank=False) description = models.TextField(null=False, blank=False) class AreaPoint(models.Model): x_axis = models.FloatField(default=0.0) y_axis = models.FloatField(default=0.0) area = models.OneToOneField(Area,primary_key=True,on_delete=models.CASCADE) I try two method , but both fail ,please guide me. thank you # first method : # Area.objects.filter(id=304).update(area_name="today is 1",

Save the user from one model to the another model

你离开我真会死。 提交于 2019-12-08 02:38:07
问题 What I want to do is, whenever I create a new message, I want the sender of the Message to be added to the user of that particular Thread (the Message its relating to). How do I do that? Can it be done by overriding the save method? I can do it in the views.py, but I was hoping it would be better if I can add it in the models.py itself. Any help will be very grateful. Thank you! class Thread(models.Model): user = models.ManyToManyField(User) is_hidden = models.ManyToManyField(User, related

In a Django subquery, can I reference the “parent” query?

半腔热情 提交于 2019-12-08 02:07:30
问题 It's simple to create subqueries in Django ORM (just use a QuerySet as part of another query), but is it possible for that subquery to reference fields in the "parent" (outer, main) query? For a full example of what I'm trying to achieve, see this working SQL Fiddle. I broke it down into two questions (other one here). In this case, I have a model Whole that represents a value that must be reached. Several Part s contribute to it with a (calculated) value of their own. I want to retrieve all

Django ORM equivalent for this SQL..calculated field derived from related table

可紊 提交于 2019-12-08 01:32:37
问题 I have the following model structure below: class Master(models.Model): name = models.CharField(max_length=50) mounting_height = models.DecimalField(max_digits=10,decimal_places=2) class MLog(models.Model): date = models.DateField(db_index=True) time = models.TimeField(db_index=True) sensor_reading = models.IntegerField() m_master = models.ForeignKey(Master) The goal is to produce a queryset that returns all the fields from MLog plus a calculated field (item_height) based on the related data

django.db.utils.IntegrityError: (1062, “Duplicate entry '22-add_' for key 'content_type_id'”)

强颜欢笑 提交于 2019-12-08 01:25:27
I am using django multiple DB router concepts, having multiple sites with different db's. Base database user will login with all other sub sites. When i try syncdb in base site its worked properly(at any time), but trying syncdb with other sites works first time only, if we try next time on-wards it throws integiry error like below django.db.utils.IntegrityError: (1062, "Duplicate entry '22-add_somesame' for key 'content_type_id'") Once i removed multiple DB router settings in that project means syncdb works properly(at any time). So is this relates to multiple db router? or what else? Please

Django Call Stored Procedure on Second Database

若如初见. 提交于 2019-12-07 19:46:13
问题 I'm trying to call a stored procedure on a multi-db Django installation, but am not having any luck getting results. The stored procedure (which is on the secondary database) always returns an empty array in Django, but the expected result does appear when executed in a mysql client. My view.py file from SomeDBModel import models from django.db import connection def index(request, someid): #Some related django-style query that works here loc = getLocationPath(someid, 1) print(loc) def

Django ORM: filter by hour range

对着背影说爱祢 提交于 2019-12-07 19:24:56
问题 I'm trying to implement a filter for hour range, it should returns records with a date between hourA and hourB (ie: "give me the records saved between 16pm and 18pm"). My attempts: 1) Using new 1.6 __hour filter and __in or __range : MyModel.objects.filter(date__hour__in=(16, 17, 18)) MyModel.objects.filter(date__hour__range=(16, 18)) The code above generates exceptions 2) Using Q objects: hList = [Q(date__hour=h) for h in (16, 17, 18)] MyModel.objects.filter(reduce(operator.or_, hList)) This

Django MySQL group by day with timezone

[亡魂溺海] 提交于 2019-12-07 14:16:33
问题 Lets say I have a sale model: class Sale(models.Model): total = models.DecimalField(max_digits=8, decimal_places=2, default=0) sale_date = models.DateTimeField(auto_now_add=True) Now, with each sale sale_date get its value saved in UTC, so if I try to group and sum all the sales by day: report = Sale.objects.extra({'day':"date(sale_date)"}).values('day').annotate(day_total=Sum('total')) I get all wrong becouse I expect each day in a different timezone (UTC-6). Is there a way to get the

Django orm group by multiple columns

自古美人都是妖i 提交于 2019-12-07 10:31:41
问题 How to perform a multiple column group by in Django? I have only seen examples on one column group by. Below is the query i am trying to convert to Django ORM. SELECT order_id,city,locality,login_time,sum(morning_hours),sum(afternoon_hours),sum(evening_hours),sum(total_hours) FROM orders GROUPBY order_id,city,locality,login_time` 回答1: from django.db.models import Sum Your_Model.objects.values('order_id', 'city', 'locality', 'login_time').order_by().annotate(sum('morning_hours'), sum(

Django, queryset filter ManyToManyField

柔情痞子 提交于 2019-12-07 10:18:47
问题 I do have the two models below. So I'm trying to get all the modules of a particular course. As you can see, I'm already getting that particular course. So I just need to get the modules from it. I read the docs about filtering a ManyToManyField but still couldn't make it work. I know that maybe it's too simple but can't solve it. models.py class Course(models.Model): name = models.CharField(max_length=100) modules = models.ManyToManyField('Module', blank=True) class Module(models.Model):