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.date.today() - datetime.timedelta(days=30))

e.t.c

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 manage.py makemigrations APP_NAME
python manage.py sqlmigrate APP_NAME 0001
python manage.py migrate

Next, in your models file:

timestamp = models.DateField(("Date"), default=datetime.date.today)

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. https://docs.djangoproject.com/en/3.0/ref/models/fields/#datefield

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:

html:

{% if model %}
    <table>
        <thead>
            <tr>
                <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>
            </tr>
        </thead>
        <tbody>
        {% for i in model %}
            <tr>
                <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;">{{ i.credit }}</td>
                <td style="text-align: center;">{{ i.timestamp }}</td>
            </tr>
        {% endfor %}
        </tbody>
    </table>
    {% else %}
        <p>There are no active transactions!</p>
    {% endif %}

Views.py

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 = datetime.date.today-timestamp(days=30))
            elif date_id == 2:
                d = transaction.objects.filter(emp_id = x, timestamp = datetime.date.today-timestamp(days=60))
            else:
                d = transaction.objects.filter(emp_id = x, timestamp = datetime.date.today-timestamp(days=180))
                print(d)

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

models.py:

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"), default=datetime.date.today)

Any help is appreciated.

Comments

Comment posted by DHRUV KAUSHAL

Thank you. This resolved my issue on the views.py. 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 😀

By