I have a Django model for an object with a DateField. I also have two managers that filter these objects for a specific date range.
class SchoolYearManager(models.Manager):
def get_query_set(self):
now = datetime.datetime.now()
current_year = now.year
start_date = datetime.date(current_year, 7, 1)
end_date = datetime.date((current_year + 1), 6, 30)
return super(SchoolYearManager, self).get_query_set().filter(status=self.model.LIVE).filter(event_date__range=(start_date, end_date))
class PastSchoolYearManager(models.Manager):
def get_query_set(self):
current_year = self.model.event_date.year
start_date = datetime.date(current_year, 7, 1)
end_date = datetime.date((current_year + 1), 6, 30)
return super(PastSchoolYearManager, self).get_query_set().filter(status=self.model.LIVE).filter(event_date__range=(start_date, end_date))
class Event(models.Model):
LIVE = 3
DRAFT = 4
STATUS_CHOICES = (
(LIVE, 'Live'),
(DRAFT, 'Draft'),
)
status = models.IntegerField(choices=STATUS_CHOICES, default=4, help_text="Only entries with a status of 'live' will be displayed publically.")
event_date = models.DateField()
objects = Models.Manager()
school_year_events = SchoolYearManager()
past_school_year_events = PastSchoolYearManager()
My first manager (SchoolYearManager) works as expected to return events within that date range. But when I try to do Event.past_school_year_events.all(), I get an Attribute error: "type object 'Event' has no attribute 'event_date'".
My goal with the second manager (PastSchoolYearEvents) is to wrap a generic year archive view to return events within a date range for a specific year.
Why can't I call self.model.event_date within the manager?
Am I going about this the right way? If not, what's the better way to do this?
How could this work? What event date could it be referring to? When you call Event.past_school_year_events.all(), you don't have a model instance. self.model, as the name implies, refers to the model class. So how could it know what date you mean?
I can't actually work out what it is you're trying to do - where you are going to get the date from. Do you want to start from an event, then get all the other events in the same year as that one? In which case, I suspect you just want to make past_school_year_events into a model method, not a manager.
Edit If you want to start from a specific year, a manager certainly is appropriate, but you'll need to pass the year into your manager method as a parameter. For this, I'd use a separate method on the manager, rather than overriding get_query_set - in fact, add another method to SchoolYearManager:
class SchoolYearManager(models.Manager):
def live_events(self, start_date, end_date):
return self.filter(status=self.model.LIVE).filter(event_date__range=(start_date, end_date))
def this_year(self):
now = datetime.datetime.now()
current_year = now.year
start_date = datetime.date(current_year, 7, 1)
end_date = datetime.date((current_year + 1), 6, 30)
return self.live_events(start_date, end_date)
def from_year(self, year):
start_date = datetime.date(year, 7, 1)
end_date = datetime.date(year+1, 6, 30)
return self.live_events(start_date, end_date)
Now you've got just one manager, that doesn't override get_query_set, so you don't even need to preserve the default one - you can keep this one as objects. So you can do:
Event.objects.this_year() # all this year's events
Event.objects.from_year(2010) # all events from 2010
Related
Hi i have some Django 11 project, my model look like
class Event(models.Model):
name = models.CharField(max_length=100, unique=True)
title = models.CharField(max_length=100)
info = models.CharField(max_length=100)
image = models.ImageField(upload_to='events/%Y/%m/%d')
start_date = models.DateField(default=timezone.now)
start_time = models.TimeField(default=timezone.now)
stop_date = models.DateField(default=timezone.now)
stop_time = models.TimeField(default=timezone.now)
place = models.ForeignKey('places.Place', on_delete=models.CASCADE)
company = models.ForeignKey('companies.Company', on_delete=models.CASCADE)
and my view look like
def place_website(request, place_id):
place_template = get_template('room.html')
place_obj = Place.objects.filter(id=place_id)
# filter for event obejts only for requested place, filtered for now and next events
place_event_now = Event.objects.filter(place=place_id, start_date=timezone.now().date, stop_date__gte=timezone.now().date)
place_events_next = Event.objects.filter(place=place_id, start_date=timezone.now(), stop_date__gte=timezone.now()).order_by('start_time')
place_context = {
'place_obj': place_obj,
'place_event_now': place_event_now,
'place_events_next': place_events_next,
}
return HttpResponse(place_template.render(place_context))
the thing i want to manage is to pass to template the list of filtered Event objects based on time.
Lets pick this line
place_event_now = Event.objects.filter(place=place_id, start_date=timezone.now().date, stop_date__gte=timezone.now().date)
it couse error "expected string or bytes-like object" but when i remove ".date" from "timezone.now()" error disappear (then filter do nothing) but i want to compare date to date and time to time.
How to do this properly ?
This approach to filter objects in view rather than in template is proper?
###### UPDATE ########
Its werid because after correction now i have no error but queryset is not filtered properly, look like only two first parameter is filtering ok and the another two is causing empty queryset.
place_event_now = Event.objects.filter(place=place_id, start_date=timezone.now().strftime('%Y-%m-%d'), start_time__lte=timezone.now().strftime('%H:%M:%S'), stop_time__gte=timezone.now().strftime('%H:%M:%S'))
I my database time is saved in format H:M:S and timezone.now().time() has different format so i modified filter with .strftime this didnt help, what i wont is to limit "place_event_now" queryset to particular object/objects that come true with condition start_time < currenttime < stop_time.
Another case is with place_event_next
place_events_next = Event.objects.filter(place=place_id, start_date=timezone.now().strftime('%Y-%m-%d'), stop_date__gte=timezone.now().strftime('%Y-%m-%d'), start_time__gt=timezone.now().strftime('%H:%M:%S')).order_by('start_time')
Event when i filter objects that start_time is greater than timezone.now() they still are in queryset.
Am I doing something wrong ?
I figured it out that timezone.now() return time not in my current timezone, i change it to timezone.localtime() and everything working perfect !!!
May be you need call date to date()
replace
filter(place=place_id, start_date=timezone.now().date, stop_date__gte=timezone.now().date)
# ^^^ ^^^
to
filter(place=place_id, start_date=timezone.now().date(), stop_date__gte=timezone.now().date())
# ^^^ ^^^
I'm making a website using django.
class Member(models.Model):
...
end_date = models.DateField(blank=True, default=(datetime.now() + timedelta(days=30)))
Membership_status = models.IntegerField(blank=True, null=True, default=1) # 1 = active, 0=deactivate, 2=refund
What I want to do is comparing the end_date field to today.date every 1.a.m. and if today's day < end_date, Membership_status field is changed to 0 automatically.
I heard I should use django-kronos(https://github.com/jgorset/django-kronos).
But I can't understand the using method.
Is there anyone can tell me details how I implement what I want?
Any help will be very helpful to me, thanks!
First of all, this is not an answer to your original query, but merely a suggestion for your future,
Never pass a function call into your field defaults. If you did, the function would be evaluated at the time of your migrations. If you look into the migration files you can see for sure. Instead wrap it in a function and pass that as a callable.
Eg:
from django.utils import timezone
def TODAY():
return timezone.now().date()
def NEXT_MONTH_DAY():
return TODAY() + timedelta(days=30)
Now, in your models,
class Member(models.Model):
...
end_date = models.DateField(blank=True, default=NEXT_MONTH_DAY)
This way the function NEXT_MONTH_DAY is called whenever an instance of Member is created.
EDIT:
For your original query, I haven't tested the code, but I suppose you are looking for maybe something like this,
import kronos
#kronos.register('0 1 * * *')
def the_task():
for member in Member.objects.all():
if TODAY() == member.end_date:
member.Membership_status = 0
member.save()
I have managed to create a very simple model which allows me to subtract 2 DateTime fields, like so:
class Log(models.Model):
date = models.DateField()
take = models.DateTimeField()
land = models.DateTimeField()
tot = models.CharField(max_length=200, blank=True, default='00000')
def __str__(self):
return str(self.date)
def time_delta(self):
tdelta = self.land - self.take
return str(tdelta)
def save(self, *args, **kwargs):
self.tot = self.time_delta()
super(Log, self).save(*args, **kwargs)
My problem is the user would have to specify the date on every field. How could I make the fields take and land refer to date once and for all?
I don’t know how to do that in your Django model. I think you have to calculate the dates and times in your controller instead, and then register the values in your database.
You can do something like that with datetime.datetime.combine() function:
Return a new datetime object whose date components are equal to the given date object’s, and whose time components are equal to the given time object’s.
You have a reference date, for instance: today.
import datetime
date = datetime.date.today()
The user enter the takeoff time, you can combine this time with the reference date.
takeoff_time = datetime.time(8, 12)
takeoff_datetime = datetime.datetime.combine(date, takeoff_time)
print(takeoff_datetime.isoformat(" "))
# -> 2016-12-21 08:12:00
If the landing date is the same as the takeoff date, you can calculate the landing date/time with the same date reference:
landing_time = datetime.time(12, 37)
landing_datetime = datetime.datetime.combine(date, landing_time)
print(landing_datetime.isoformat(" "))
# -> 2016-12-21 12:37:00
Then, you can register the date, _takeoff_datetime_ and _landing_datetime_ in your database.
Note: you can do the same with the flight duration
I eventually managed to find a solution largely based on Laurent's answer so here it is,if that can ever help someone else:
from datetime import datetime
def calculation(self):
calc_take_off = datetime.combine(self.date, self.take)
calc_land = datetime.combine(self.date, self.land)
result = calc_land - calc_take_off
return str(result)
and then to save this in models:
def save(self, *args, **kwargs):
self.tot = self.calculation()
super(Log, self).save(*args, **kwargs)
I need to perform a filter by date with a form where if one of the date parameters is suppressed the upper/lower border of the date range is not being set in filter (which means min possible date/max possible date, respectively)
My code is:
#forms.py
...
start_date = forms.DateField(
required=False,
)
end_date = forms.DateField(
required=False
)
...
# views.py
def get(self, *args, **kwargs):
form = self.request.GET
if form.is_valid():
cd = form.cleaned_data
if not cd['start_date']:
start_date = datetime.date(1,1,1)
else:
start_date = cd['start_date']
if not cd['end_date']:
end_date = datetime.date(3000,1,1)
else:
start_date = cd['end_date']
MyC.objects.filter(date__range=(start_date,end_date))
This code works but looks very cludgy to me (Many senseless if clauses, duplicate code, etc).
Maybe there is a Filter function for this case (if start date is None -> don't filter)?
You can apply the filters as needed. The query won't happen until you use the queryset.
objects = MyC.objects.all()
if cd['start_date']:
objects = objects.filter(date__gt=cd['start_date'])
if cd['end_date']:
objects = objects.filter(date__lt=cd['end_date'])
return objects
if you need default dates you can do:
start_date = self.cleaned_data.get('start_date', datetime.date(1,1,1))
end_date = self.cleaned_data.get('end_date', datetime.date(3000,1,1))
MyC.objects.filter(date__range=(start_date, end_date))
Have looked through DateField, TimeField, DateTimeField related documents but I cannot seem to find what I need. I simply want to have a selection of month, day, year, hour, minute, (AM/PM) type option. I have tried using 'choice=', but do not get the nice behavior I am looking for.
** TL;DR: I simply want a way of putting in the date and time without having to type it in. I would like a nice drop down menu **
class Event(models.Model):
event_name = models.CharField(max_length = 50)
date_time = models.DateTimeField()
date = models.DateField()
location = models.CharField(max_length = 30)
address = models.CharField(max_length = 30)
city = models.CharField(max_length = 30)
zip_code = models.CharField(max_length = 30)
state = models.CharField(max_length = 30)
description = models.TextField()
def __unicode__(self):
return self.event_name
class EventForm(ModelForm):
class Meta:
model = Event
This is what I currently have. I have removed the choices part and I even tried making my own model object dedicated to date and time, but that did not go well
I tried it using this ...
DATE_CHOICES = (('Jan', "January"),
('Feb', "Feburary"),
('Mar', 'March'),
('Apr','April'),
('May ','May'),
('June','June'),
('July','July'),
('Aug','August'),
('Sept','Septemeber'),
('Oct','October'),
('Nov','November'),
('Dec','December')
)
class DateTime(models.Model):
month = models.CharField(max_length = 5, choices=DATE_CHOICES)
But I am not getting the correct behavior as I want.
You might be interested in using jquery date picker or jquery datetime picker.
http://jqueryui.com/datepicker/
http://trentrichardson.com/examples/timepicker/
On both sites there are exampels so you can see it in action :)
You definitely want to use this snippet. Unless you're a django ninja and want to roll up your own multi widget, which is what you will need to transform a set of select inputs into one datetime value.
This widget is the closest you will get to do it, without using any js plugins.
Your can use datepicker plagin as Erfin mentioned (I recommend it too and also datepicker for bootstrap) for date and masked input for time or just simple selects. Anyway you should send a request with datetime information. What to do in django:
If you use datepicker plugin for date and masked input for time
Let's assume that your request is POST. Your date will be a string with format you specify in javascript. It looks like "31.01.2013". And time will be like "22:30".
def write_datetime(request):
event = Event.objects.get(id=int(request.POST.get('id')))
from datetime.datetime import strptime
date = request.POST.get('date')
time = request.POST.get('time')
date_time = date + " " + time
event.date_time = strptime(date_time, "%d.%m.%y %H:%M"
event.save()
If you use just selects
In this case, it's simplier to make a datetime string from request parameters and repeat the preveous example.
def def write_datetime(request):
year = request.POST.get('year')
month = request.POST.get('month')
# etc
date_time = "%s.%s.%s %s:%s" % (day, month, year, hours, minutes)
# etc