DHRUV KAUSHAL
DHRUV KAUSHAL

Reputation: 127

Django error stating model field not defined

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.

Upvotes: 1

Views: 1014

Answers (2)

weAreStarsDust
weAreStarsDust

Reputation: 2752

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

Upvotes: 1

crimsonpython24
crimsonpython24

Reputation: 2383

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.

Upvotes: 1

Related Questions