saving date and time of user response in django model field - django

i am making this app in django where on a particular html template if the user selects a particular response by clicking on it, the date and time at which the user clicked that particular response is stored in my database.
part of models.py
class userresp(models.Model):
rid=models.Integerfield(unique=True,default=0)
uid=models.Foreignkey(user,to_field='uid',on_delete=models.CASCADE)
resp=models.ForeignKey(elementsound,to_field='csid',on_delete=models.CASCADE)
date=models.DateTimeField()
time=models.DateTimeField()
so how do i store that? and what will be the extra parameters in the DateTimeField of both?

You can do the what you require inside a View by overriding the post() method.
To store the user's response time, you can make use of django's builtin timezone module.
So you just need to do:
from django.utils import timezone
date_and_time = timezone.now()
timezone.now() returns the system date and time of the server.
And by the way, you don't need two 'DateTimeField's to store the date and time. One is enough, it can store both the date and time.
Otherwise you can just get the current date and time at the time of userresp object creation.
class userresp(models.Model):
rid=models.Integerfield(unique=True,default=0)
uid=models.Foreignkey(user,to_field='uid',on_delete=models.CASCADE)
resp=models.ForeignKey(elementsound,
to_field='csid',
on_delete=models.CASCADE)
date_and_time=models.DateTimeField(auto_now_add=True)

Related

How to get a model's last access date in Django?

I'm building a Django application, and in it I would like to track whenever a particular model was last accessed.
I'm opting for this in order to build a user activity history.
I know Django provides auto_now and auto_now_add, but these do not do what I want them to do. The latter tracks when a model was created, and the former tracks when it was last modified, which is different from when it was last accessed, mind you.
I've tried adding another datetime field to my model's specification:
accessed_on = models.DateTimeField()
Then I try to update the model's access manually by calling the following after each access:
model.accessed_on = datetime.utcnow()
model.save()
But it still won't work.
I've gone through the django documentation for an answer, but couldn't find one.
Help would be much appreciated.
What about creating a model with a field that contains the last save-date. Plus saving the object every time is translated from the DB representation to the python representation?
class YourModel(models.Model):
date_accessed = models.DateTimeField(auto_now=True)
#classmethod
def from_db(cls, db, field_names, values):
obj = super().from_db(db, field_names, values)
obj.save()
return obj

Django DateTimeField hide date

I have a model where an object is called time which is model.DateTimeField(default=datetime.now)
I also have a form where the user can specify the time, but I wanted to only have the H: M in there, not the whole datetime.
I tried using the TimeInput widget in my form, but that would give me an error upon submitting because I didn't have the 'date' portion of the DateTimeField. If I use models.TimeField for time, I lose the ability to keep track of the date in the admin page.
How can I hide the date from showing in my form?
Worked around the problem by having one model as DateTimeField and another as TimeField.

how to create a date that doesn't change on a Django webpage

I created a webpage in Django that shows a list of items from a database. I would like to create a feature where when a user first generates that page, the current date (not time) appears on the page. The hard part is that when the user shares a link to that page on social media or wherever, the original date should remain unchanged. In other words, it should not change each time a subsequent user views the page through the link shared by the first user. The date is only created once when the first user generates the page.
The purpose is to show subsequent viewers of that page all the relevant database items as of the date the first user accessed them. But then I will also provide information about the number of additional relevant entries that have been made to the database since the date the first user accessed them. Subsequent users would then have the option of going to a new page that shows all database items as of today. How can I implement this?
You can track when the user generated a page using a model. One of the field for that model should be 'created' as shown below(of course the name can be anything). Then override the save method of that model to update the created field only at initial creation. (This is a standard technique used in django to track item creation date)
from datetime import date
class Yourmodel(models.Model):
created = models.DateField()
def save(self, *args, **kwargs):
''' On save, update timestamps '''
if not self.id:
self.created = date.today()
return super(Yourmodel, self).save(*args, **kwargs)

Django - order queryset result by update time of instances

Is there a way to order result of a model queryset by update time of its instances? i.e. the instance that has been saved most recently comes first and so on.
You will need to add a timestamp field to your model. For example in my own code I add a date_updated field for this very purpose.
Your custom models don't have this by default so you have to add it.
last_edit = models.DateTimeField(auto_now_add=True)
You will have to update this in the save method (or another method) of your model.
import datetime
self.last_edit = datetime.datetime.now()
If you have a field on your model that tracks this update time (more information on this here), then yes, you can just use that in a normal ordering specification.
If you don't track updates on your model, no, that is not possible.

Does Django DateTimeField supporting time with time zone information?

When I post a time with timezone information eg:2013-02-27T14:00:00-05:00 to a Django datetime field throws a form error "Enter a valid date/time.".
My form field is defined as below
time = forms.DateTimeField()
I also tried passing in date formats to the form filed. eg:
DATE_FORMATS = [
'%Y-%m-%dT%H:%M:%S-%z',
]
time = forms.DateTimeField(input_formats=DATE_FORMATS)
Does Django DateTimeField not support time with timezone information?
Yes! Django DateTimeField supports timezones, but if you take a look at the documentation, you have to specify it separately from the actual DateTime in your form. Specifically, the documentation recommends that you activate a timezone on a per user basis, otherwise you will always end up entering the DateTime in the default timezone.
https://docs.djangoproject.com/en/dev/topics/i18n/timezones/#time-zone-aware-input-in-forms
This is the documentation I used for my project, and it worked out well.
In addition, if you need to specify a specific timezone in a form field, you can always create a drop down field with all of the timezones in it, and then save the DateTime in your view with the correct timezone.