2

I am building a survey website using Django 1.7.7 and Python 3.4.4, where a user enters say his "name" and "id". I have a sql table called "list" that stores all the users names and id's. I would like my Django website to check if when a user enters his/her name and id, if the id already exists in the table for the past 30 days. Basically if the user's id exists in the past 30 days, he/she will not be prompted to take the survey, else they would have to take the survey.

I know I have to build a view function that checks this condition but I am unable to figure out what the query/logic should be. Any help on the view function would help me out in my development immensely! Thank you.

AK2426
  • 53
  • 1
  • 5
  • 1
    seems like you need to add a timestamp to your database that you update as needed. Then you can check the timestamp as needed . – DCR Jan 03 '17 at 18:47
  • In the "list" table, do you have a column that records the date of a submission? Your database does not automatically record that data, you have to add a datetime column and update your submission process accordingly. After that, you can just use Django's ORM to query & filter by time. Check this solution for an example of querying by date range: http://stackoverflow.com/a/6194999/844976 – YellowShark Jan 03 '17 at 18:52

1 Answers1

1

You can add "timestamp" field to your List model. You can even make Django automatically set it to now() using auto_now_add parameter

 timestamp = models.DateTimeField(auto_now_add=True)

Than you can check if there are records with the same name and id within last month:

 from django.utils.timezone import now
 import datetime
 is_duplicate = List.objects.filter(id=id, name=name, timestamp__gte = now() - datetime.timedelta(days=30))

Note usages of __gte construtions - it means "greater than", and can be used with numbers and timestamps. This filter will only return records created within past 30 days. Hope this helps!