Questions tagged [django-database-functions]

21 questions
22
votes
1 answer

How to make/use a custom database function in Django

Prologue: This is a question arising often in SO: Equivalent of PostGIS ST_MakeValid in Django GEOS Geodjango: How to Buffer From Point Get random point from django PolygonField Django custom for complex Func (sql function) and can be applied to…
John Moutafis
  • 22,254
  • 11
  • 68
  • 112
6
votes
4 answers

How to annotate a difference of datetime in days

I have a Booking model that has start and end datetime fields. I want to know how many days a booking covers. I can do this in Python but I need this value for further annotations. Here's what I've tried: In [1]:…
Oli
  • 235,628
  • 64
  • 220
  • 299
4
votes
0 answers

Django group by consecutive days (Friday, Saturday, Sunday)

I have an event that occurs everyday. Here is the model class Event(models.Model): date = models.DateField() plays = models.IntegerField() viewers = models.IntegerField() time = models.FloatField() I am trying to aggregate the…
3
votes
0 answers

How to filter an annotated queryset by - Window function - without changing the value of the annotated field

I have a queryset of users, after annotating the rank of each user using Django Window function, I want to query for a user without modifying the rank value users_points_query = users.order_by( '-total_points' ).annotate( …
3
votes
1 answer

Filtering down through multiple ForeignKey relations in django

I am trying to get down through multiple-foreign key relationship where each Hotel has many Rooms, each Room has many Rateplans, each Rateplan has many Prices. It resembles Christmas tree if you think about it: Hotel v Room v Rateplan v Prices How…
3
votes
1 answer

Django bulk update with data two tables over

I want to bulk update a table with data two tables over. A solution has been given for the simpler case mentioned in the documentation of: Entry.objects.update(headline=F('blog__name')) For that solution,…
AlanSE
  • 2,597
  • 2
  • 29
  • 22
1
vote
1 answer

django orm - annotate / aggregation (avg) in subquery

I have this model: class UserMovieRel(models.Model): user = models.ForeignKey("register.User", on_delete=models.CASCADE) movie = models.ForeignKey("Movie", on_delete=models.CASCADE, related_name="users") rating =…
1
vote
2 answers

SOLVED: Django ORM: How to round down (truncate) a query number?

SOLVED I'm working with sensitive currency values. In my case, i have to reproduce a sheet with it's formulas. The point is that i need to round down a currency value with 2 decimal places. A practical example is the number: 9809.4069, it should be…
1
vote
1 answer

How to get table data (including child table and sub child data) based on id which obtains from another table data? Django

views company = Company.objects.get(id = company_id) # getting input from django urls () vehicles = CompanyContainVehicles.objects.filter(company_id=company.id) # Give all rows having same id (company.id) all_vehicles =…
1
vote
1 answer

Filter an object that has multiple related objects in Django

Let's say I have two models that have one-to-many relationships as the code below. I'd like to only get order objects that have more than one shipment object. The only way I can think of is getting it through a list comprehension [order for order in…
1
vote
2 answers

Django annotate + SUM how to get all entries

My models class Machine(models.Model): machineName = models.CharField(verbose_name="Machine Name", max_length=20, blank=False, null=False) class SalesReport(models.Model): machine = models.ForeignKey(Machine, on_delete=models.CASCADE,…
1
vote
2 answers

while connecting to MicrosoftSQL server using Django facing django.db.utils.OperationalError:

drivers available with me **python shell** '''In [2]: pyodbc.drivers()''' **Output:** **Out[2]: ['SQL Server']** code in settings.py django: **Settings.py in django** '''# Database #…
1
vote
1 answer

Compare year from DateTimeField/DateField Django ORM

I have a model configuration as below, class Foo(models.Model): start = models.DateTimeField() end = models.DateTimeField() How can I retrieve Foo instances with same Year? Unsuccessfull try: from django.db.models import…
JPG
  • 82,442
  • 19
  • 127
  • 206
1
vote
1 answer

How to link existing tables in database to Django Models?

I am learning Django and i need to know whether i can link a table already present in the database to a model (since, i heard that tables are created by Django for whatever models we are creating inside django app ). If so, how can it be done?
0
votes
1 answer

Changing Year, Month, or Day in a Django model's date field using annotate

I have a Django model MyModel class MyModel(models.Model): """My Model that stores date.""" date = models.DateField() In my API I am receiving a param, review_month I need to get all the entries in MyModel and replace their date's year…
1
2