Django count grouping by year/month without extra - python

I'm working with django 1.9
Model :
class Comment(models.Model):
title = models.CharField(max_length=250, null=False)
date = models.DateField(auto_now_add=True)
As 'extra()' will be deprecated in django i try to figure out how to count Comments group by year-month without using 'extra'
Here is the code with extra :
Comment.objects.extra(select={'year': "EXTRACT(year FROM date)",
'month': "EXTRACT(month from date)"})\
.values('year', 'month').annotate(Count('pk'))
Thank you for your help.

See year and month in the docs, may be something like the following will do the job:
Comment.objects.annotate(year=Q(date__year),
month=Q(date__month)
).values('year', 'month').annotate(Count('pk'))
If this won't work, then instead of Q(date__year), you could define a custom Func() expression representing EXTRACT(year FROM date) function and use it in annotate(). Or, well, as last resort there's RawSQL().
Using Func(), something like this:
from django.db.models import Func
class Extract(Func):
"""
Performs extraction of `what_to_extract` from `*expressions`.
Arguments:
*expressions (string): Only single value is supported, should be field name to
extract from.
what_to_extract (string): Extraction specificator.
Returns:
class: Func() expression class, representing 'EXTRACT(`what_to_extract` FROM `*expressions`)'.
"""
function = 'EXTRACT'
template = '%(function)s(%(what_to_extract)s FROM %(expressions)s)'
#Usage
Comment.objects.annotate(year=Extract(date, what_to_extract='year'),
month=Extract(date, what_to_extract='month')
).values('year', 'month').annotate(Count('pk'))

Related

Associate classes with django-filters

Bonjour, I have a question regarding django-filters. My problem is:
I have two classes defined in my models.py that are:
class Volcano(models.Model):
vd_id = models.AutoField("ID, Volcano Identifier (Index)",
primary_key=True)
[...]
class VolcanoInformation(models.Model):
# Primary key
vd_inf_id = models.AutoField("ID, volcano information identifier (index)",
primary_key=True)
# Other attributes
vd_inf_numcal = models.IntegerField("Number of calderas")
[...]
# Foreign key(s)
vd_id = models.ForeignKey(Volcano, null=True, related_name='vd_inf_vd_id',
on_delete=models.CASCADE)
The two of them are linked throught the vd_id attribute.
I want to develop a search tool that allows the user to search a volcano by its number of calderas (vd_inf_numcal).
I am using django-filters and for now here's my filters.py:
from .models import *
import django_filters
class VolcanoFilter(django_filters.FilterSet):
vd_name = django_filters.ModelChoiceFilter(
queryset=Volcano.objects.values_list('vd_name', flat=True),
widget=forms.Select, label='Volcano name',
to_field_name='vd_name',
)
vd_inf_numcal = django_filters.ModelChoiceFilter(
queryset=VolcanoInformation.objects.values_list('vd_inf_numcal', flat=True),
widget=forms.Select, label='Number of calderas',
)
class Meta:
model = Volcano
fields = ['vd_name', 'vd_inf_numcal']
My views.py is:
def search(request):
feature_list = Volcano.objects.all()
feature_filter = VolcanoFilter(request.GET, queryset = feature_list)
return render(request, 'app/search_list.html', {'filter' : feature_filter, 'feature_type': feature_type})
In my application, a dropdown list of the possible number of calderas appears but the search returns no result which is normal because there is no relation between VolcanoInformation.vd_inf_numcal, VolcanoInformation.vd_id and Volcano.vd_id.
It even says "Select a valid choice. That choice is not one of the available choices."
My question is how could I make this link using django_filters ?
I guess I should write some method within the class but I have absolutely no idea on how to do it.
If anyone had the answer, I would be more than thankful !
In general, you need to answer two questions:
What field are we querying against & what query/lookup expressions need to be generated.
What kinds of values should we be filtering with.
These answers are essentially the left hand and right hand side of your .filter() call.
In this case, you're filtering across the reverse side of the Volcano-Volcano Information relationship (vd_inf_vd_id), against the number of calderas (vd_inf_numcal) for a Volcano. Additionally, you want an exact match.
For the values, you'll need a set of choices containing integers.
AllValuesFilter will look at the DB column and generate the choices from the column values. However, the downside is that the choices will not include any missing values, which look weird when rendered. You could either adapt this field, or use a plain ChoiceFilter, generating the values yourself.
def num_calderas_choices():
# Get the maximum number of calderas
max_count = VolcanoInformation.objects.aggregate(result=Max('vd_inf_numcal'))['result']
# Generate a list of two-tuples for the select dropdown, from 0 to max_count
# e.g, [(0, 0), (1, 1), (2, 2), ...]
return zip(range(max_count), range(max_count))
class VolcanoFilter(django_filters.FilterSet):
name = ...
num_calderas = django_filters.ChoiceFilter(
# related field traversal (note the connecting '__')
field_name='vd_inf_vd_id__vd_inf_numcal',
label='Number of calderas',
choices=num_calderas_choices
)
class Meta:
model = Volcano
fields = ['name', 'num_calderas']
Note that I haven't tested the above code myself, but it should be close enough to get you started.
Thanks a lot ! That's exactly what I was looking for ! I didn't understand how the .filter() works.
What I did, for other attributes is to generate the choices but in a different way. For instance if I just wanted to display a list of the available locations I would use:
# Location attribute
loc = VolcanoInformation.objects.values_list('vd_inf_loc', flat=True)
vd_inf_loc = django_filters.ChoiceFilter(
field_name='vd_inf_vd_id__vd_inf_loc',
label='Geographic location',
choices=zip(loc, loc),
)

How to create a custom AutoField primary_key entry within Django

I am trying to create a custom primary_key within my helpdesk/models.py that I will use to track our help desk tickets. I am in the process of writing a small ticking system for our office.
Maybe there is a better way? Right now I have:
id = models.AutoField(primary_key=True)
This increments in the datebase as; 1, 2, 3, 4....50...
I want to take this id assignment and then use it within a function to combine it with some additional information like the date, and the name, 'HELPDESK'.
The code I was using is as follows:
id = models.AutoField(primary_key=True)
def build_id(self, id):
join_dates = str(datetime.now().strftime('%Y%m%d'))
return (('HELPDESK-' + join_dates) + '-' + str(id))
ticket_id = models.CharField(max_length=15, default=(build_id(None, id)))
The idea being is that the entries in the database would be:
HELPDESK-20170813-1
HELPDESK-20170813-2
HELPDESK-20170814-3
...
HELPDESK-20170901-4
...
HELPDESK-20180101-50
...
I want to then use this as the ForeignKey to link the help desk ticket to some other models in the database.
Right now what's coming back is:
HELPDESK-20170813-<django.db.models.fields.AutoField>
This post works - Custom Auto Increment Field Django Curious if there is a better way. If not, this will suffice.
This works for me. It's a slightly modified version from Custom Auto Increment Field Django from above.
models.py
def increment_helpdesk_number():
last_helpdesk = helpdesk.objects.all().order_by('id').last()
if not last_helpdesk:
return 'HEL-' + str(datetime.now().strftime('%Y%m%d-')) + '0000'
help_id = last_helpdesk.help_num
help_int = help_id[13:17]
new_help_int = int(help_int) + 1
new_help_id = 'HEL-' + str(datetime.now().strftime('%Y%m%d-')) + str(new_help_int).zfill(4)
return new_help_id
It's called like this:
help_num = models.CharField(max_length=17, unique=True, default=increment_helpdesk_number, editable=False)
If gives you the following:
HEL-20170815-0000
HEL-20170815-0001
HEL-20170815-0002
...
The numbering doesn't start over after each day, which is something I may look at doing. The more I think about it; however, I am not sure if I even need the date there as I have a creation date field in the model already. So I may just change it to:
HEL-000000000
HEL-000000001
HEL-000000002
...

Python/Django date query: Unsupported lookup 'date' for DateField or join on the field not permitted

I have a simple method. Entries are entries in a time sheet application where employees enter their hours.
class Entry(m.Model):
""" Represents an entry in a time_sheet. An entry is either for work, sick leave or holiday. """
# type choices
WORK = 'w'
SICK = 's'
VACATION = 'v'
type_choices = (
(WORK, 'work'),
(SICK, 'sick leave'),
(VACATION, 'vacation'),
)
# meta
cr_date = m.DateTimeField(auto_now_add=True, editable=False, verbose_name='Date of Creation') # date of creation
owner = m.ForeignKey(User, editable=False, on_delete=m.PROTECT)
# content
type = m.CharField(max_length=1, choices=type_choices, default='w')
day = m.DateField(default=now)
start = m.TimeField(blank=True) # starting time
end = m.TimeField(blank=True) # ending time
recess = m.IntegerField() # recess time in minutes
project = m.ForeignKey(Project, on_delete=m.PROTECT)
#classmethod
def get_entries_for_day(cls, user, day):
""" Retrieves any entries for the supplied day. """
return Entry.objects.filter(day__date=day, owner=user).order_by('start')
However, when I try to run my project like this, it terminates with the following error code:
"Unsupported lookup 'date' for DateField or join on the field not
permitted."
I don't quite understand the message. The specified field is a date field which has no further restrictions. Any hints would be appreciated.
There's no such thing as a __date lookup on a DateField; the field is already a date.
It's not clear what you are trying to compare this field with. Is the day you are passing into that method an integer, or a date? If it's also a date then you should just compare them directly.
I'm facing an issue with Django-filters, The filter was not taking the same date range while I was using it. so I added date__lte/gte in lookup_expr.something like this.
from_date = django_filters.DateFilter(field_name="created_at", lookup_expr='date__gte')
to_date = django_filters.DateFilter(field_name="created_at", lookup_expr='date__lte')

How to use django_filters.DateFilter?

I'm trying to use django_filter's DateFilter to filter by an exact date, but could not get it to return any result.
myapp/models.py
from django.db import models
class Event(models.Model):
start = models.DateField()
title = models.CharField(_'title'), max_length=256)
myapp/filters.py
from myapp.models import Event
import django_filters
class EventFilter(django_filters.FilterSet):
start = django_filters.DateFilter(
'start', label=_('With start date'),
widget=forms.DateInput() # I'm using a datepicker-like widget to enter date
)
class Meta:
model = Event
fields = ('start',)
For example:
I have an event with start date 01/14/2012. When I entered that value in the date filter, it returns nothing.
I wasn't able to find a more in-depth description about DateFilter in django_filter's documentation site, either in its filter reference or Using django-filter guide. Or anywhere in general. Any ideas what I might be doing wrong?
I don't expect there's anything wrong with the date format from the datepicker widget since it's being used in another form (to enter data) and it works fine.
Additional info: I'm using django-1.6 and whatever the last version of django-filter
Found the issue.
The 'start' field is a DateTimeField. So when querying only for the date, no match is found because it never matches the time.
For example:
If I enter 01/14/2012, it looks for start date datetime.date(2012, 01, 14, 0, 0), but the actual start date may be datetime.datetime(2012, 01, 14, 21, 0, tzinfo=<UTC>).
Solution:
Use lookup_type='startswith' or 'lookup_type='contains' (source) but 'contains' appears to be faster
Class EventFilter(django_filters.FilterSet):
start = django_filters.DateFilter(
'start', label=_('With start date'),
lookup_type='contains' # use contains
)
...
You can filter datetime field by date
start = django_filters.DateFilter('start__date')

Django IntegerField returns ExpressionNode

I got a model containing two counters :
class MyObject(models.Model):
...
student_c = models.PositiveIntegerField(default=0, blank=True, null=True)
company_c = models.PositiveIntegerField(default=0, blank=True, null=True)
def total_followers(self):
return self.student_c + self.company_c
one is working perfectly and returns an integer value but the other one returns :
django.db.models.expressions.ExpressionNode object at 0x7ff4d8320a10
I tried to change PositiveIntegerField to IntegerField and to rename the field with another name but I still get the same result ? I destroyed the database and rebuilt it many times
In my template I can see this : (+: (DEFAULT: ), 1)
In fact I'm trying to do an atomic increment like this:
project = get_object_or_404(Project, pk=kwargs['pk'])
project.student_c = F('student_c') + 1
project.save(update_fields=['student_c']) or even just project.save()
This is where the problem is coming from. I looked there and I found the same code :
Atomic increment of a counter in django
What am I doing wrong ?
Any help would be very appreciated.
Thanks
I'm not sure you're using the F() object correctly. Have you tried just doing
Projects.objects.filter(pk=kwargs['pk']).update(student_c=F('student_c') + 1)
That would replace those three lines. Also you could try this for the second line:
project.student_c = project.student_c + 1
I don't know if this is similar to your problem, but maybe can help you
http://www.voidspace.org.uk/python/weblog/arch_d7_2011_04_30.shtml
Maybe you need to call .save before call total_followers in your view...
For example:
import model.MyObject
myObj = MyObject.objects.create(....)
myObj.save()
myObj.total_followers()

Categories