Solution 1 :

If i understand you right, you want filter transaction if their timestamp date less than 30, 60, 180 days from today

Then your filter should look like this

d = transaction.objects.filter(emp_id = x, timestamp__gte = - datetime.timedelta(days=30))


p.s.: Avoid naming models field with names that used in libs that you use, like timestamp

Solution 2 :

First things first, when Django raised an undefined error, are you sure that you applied migrations to this application?

python makemigrations APP_NAME
python sqlmigrate APP_NAME 0001
python migrate

Next, in your models file:

timestamp = models.DateField(("Date"),

When I looked up Django’s model field reference, I found this:

class DateField(auto_now=False, auto_now_add=False, **options)

After reviewing your needs, will it help when you switch auto_now_add to True? I’m doubtful that default= will work in every circumstance.

As a side note, when you create an object in the admin dashboard, sometimes Django will mischeck field requirements (like creating superusers), so please do watch out those objects in the management site.

Problem :

So I am trying to create a django view where retrieval is done on the basis of past number of months for the current logged in user, as selected by him/her in the radio choices. But I am facing an error stating that one of my model fields ‘timestamp’ is not defined. Also, I am a bit confused as to how to print the retrieved model in the html. Given below are my files:


{% if model %}
                <th style = "padding: 20px;">Vendor ID</th>
                <th style = "padding: 20px;">Employee ID</th>
                <th style = "padding: 20px;">Debit</th>
                <th style = "padding: 20px;">Credit</th>
                <th style = "padding: 20px;">Time of transaction</th>
        {% for i in model %}
                <td style="text-align: center;">{{ i.vendor_id }}</td>
                <td style="text-align: center;">{{ i.emp_id }}</td>
                <td style="text-align: center;">{{ i.debit }}</td>
                <td style="text-align: center;">{{ }}</td>
                <td style="text-align: center;">{{ i.timestamp }}</td>
        {% endfor %}
    {% else %}
        <p>There are no active transactions!</p>
    {% endif %}

if 'form2' in request.POST:
            d = {}
            date_id = request.POST["groupOfDefaultRadios1"]
            x = employee.objects.get(name = request.user)
            if date_id == 1:
                d = transaction.objects.filter(emp_id = x, timestamp =
            elif date_id == 2:
                d = transaction.objects.filter(emp_id = x, timestamp =
                d = transaction.objects.filter(emp_id = x, timestamp =

            return render(request, 'profiles/userLogin.html', {'model':d})

class vendor(models.Model):
    id = models.CharField(max_length=20, primary_key=True)
    name = models.CharField(max_length=30)

class employee(models.Model):
    name = models.OneToOneField(User, on_delete=models.CASCADE)
    id = models.CharField(max_length=20, primary_key=True)
    balance = models.IntegerField(default=0)

class transaction(models.Model):
    vendor_id = models.ForeignKey(vendor, on_delete=models.CASCADE)
    emp_id = models.ForeignKey(employee, on_delete=models.CASCADE)
    debit = models.IntegerField()
    credit = models.IntegerField()
    timestamp = models.DateField(("Date"),

Any help is appreciated.


Comment posted by DHRUV KAUSHAL

Thank you. This resolved my issue on the By the way, why should we avoid naming models field with names that used in libs that we use? How is timestamp_gte getting matched to model if I haven’t specified the field?

Comment posted by weAreStarsDust

Try to search answer on your quesion, and if fail, formulate and create new question. P.s.: look in the direction of

Comment posted by weAreStarsDust

You edited your comment so mine become not relevant now. About naming avoid. You got error that problem in your model field, because your model field called

Comment posted by DHRUV KAUSHAL

Got it now. And sorry about editing my comment, I got to the solution before reading your comment 😀