#31389: Django SQLite3 ExtractWeek returns week one instead of the last week of 
the
year
-------------------------------------+-------------------------------------
               Reporter:  dmoetech   |          Owner:  nobody
                   Type:  Bug        |         Status:  new
              Component:  Database   |        Version:  3.0
  layer (models, ORM)                |       Keywords:  sqlite3 extraxweek
               Severity:  Normal     |  query
           Triage Stage:             |      Has patch:  0
  Unreviewed                         |
    Needs documentation:  0          |    Needs tests:  0
Patch needs improvement:  0          |  Easy pickings:  0
                  UI/UX:  0          |
-------------------------------------+-------------------------------------
 I'm using: SQLite3 and Django 3.03

 I've got the following query:
 {{{
 self.changes
             .annotate(week=ExtractWeek('date'),
                       year=ExtractYear('date'))
             .values('year', 'week')
             .annotate(balance=Avg('balance'))
             .values('year', 'week', 'balance', 'date')
             .order_by('year', 'week')
 }}}


 The Change model looks like this:
 {{{
 class Change(models.Model):
     account = models.ForeignKey(Account, on_delete=models.CASCADE,
 related_name="changes")
     date = models.DateTimeField()
     category = models.ForeignKey(Category, related_name="changes",
 null=True,
                                  on_delete=models.SET_NULL)
     description = models.TextField(blank=True)
     change = models.DecimalField(decimal_places=2, max_digits=15)
     # query optimization
     balance = models.DecimalField(max_digits=15, decimal_places=2,
 null=True, blank=True)
 }}}


 I've got a change with the following date:
 {{{
 datetime.datetime(2019, 12, 30, 15, 12, tzinfo=<UTC>)
 }}}
 Now the query turns this date into:
 {{{
 {'week': 1, 'year': 2019, 'balance': Decimal('7488.74000000000')}
 }}}
 That makes no sense, as the week should be 52 or 53.


 I tried the following:
 {{{
 select *, strftime('%W', date) from banking_change where id=2742 limit 1;
 returns week: 52
 }}}

 {{{
 datetime(2019, 12, 30, 15, 12, tzinfo=pytz.utc).strftime('%W')
 returns week: 52
 }}}

 It doesn't matter if the datetime is localized to Europe/Berlin before.


 Why does django say that the week is 1? Seems like a bug to me.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/31389>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/051.ee9852a06933bb4b469f392f78ec7e47%40djangoproject.com.

Reply via email to