Error with filter queryset

639 views
Skip to first unread message

Chuck G. Madamombe

unread,
Feb 9, 2020, 7:59:33 AM2/9/20
to Django users
Hello,

I want to filter data in the database to retrieve only those tasks with status='In progress'. I have used the filter queryset, but am getting an error. Please help.

Here is my view (Filter tasks by their status):

def tasks_in_progress(request):
tasks = Task.objects.filter(Status='In progress')
context = {'tasks': tasks}
#return render(request,"todoapp/show_task.html",{'tasks':tasks})
return render(request,"todoapp/tasks_in_progress.html", context)


The model.py
class Task(models.Model):
#Task_Title = models.ForeignKey(ToDoList, on_delete=models.CASCADE)
Task_Title = models.CharField(max_length=500)
Task_Description = models.CharField(max_length=500)
Priority = models.ForeignKey(Task_Priority, on_delete=models.CASCADE)
Date_Created = models.DateTimeField(auto_now_add=True)
Completion_Date = models.DateTimeField()
Assigned_To = models.ForeignKey(User, blank=True, null=True, on_delete=models.SET_NULL)
Status = models.ForeignKey(Task_Status, on_delete=models.CASCADE)

def __str__(self):
return self.Task_Description
The error am getting is:

ValueError at /todoapp/tasks_in_progress

Field 'id' expected a number but got 'In progress'.
Request Method:GET
Request URL:http://127.0.0.1:8000/todoapp/tasks_in_progress
Django Version:3.0.2
Exception Type:ValueError
Exception Value:
Field 'id' expected a number but got 'In progress'.
Exception Location:C:\Users\Chuck Madamombe\Desktop\djangoPROJECTS\lib\site-packages\django\db\models\fields\__init__.py in get_prep_value, line 1772
Python Executable:C:\Users\Chuck Madamombe\Desktop\djangoPROJECTS\Scripts\python.exe
Python Version:3.7.1
Python Path:
['C:\\Users\\Chuck Madamombe\\Desktop\\djangoPROJECTS',
 'C:\\Users\\Chuck Madamombe\\Desktop\\djangoPROJECTS\\Scripts\\python37.zip',
 'C:\\Users\\Chuck '
 'Madamombe\\AppData\\Local\\Programs\\Python\\Python37-32\\DLLs',
 'C:\\Users\\Chuck '
 'Madamombe\\AppData\\Local\\Programs\\Python\\Python37-32\\lib',
 'C:\\Users\\Chuck Madamombe\\AppData\\Local\\Programs\\Python\\Python37-32',
 'C:\\Users\\Chuck Madamombe\\Desktop\\djangoPROJECTS',
 'C:\\Users\\Chuck Madamombe\\Desktop\\djangoPROJECTS\\lib\\site-packages',
 'C:\\Users\\Chuck '
 'Madamombe\\Desktop\\djangoPROJECTS\\lib\\site-packages\\setuptools-39.1.0-py3.7.egg']
Server time:Sun, 9 Feb 2020 10:22:05 +0200


But if I just retrieve all the records in the database, without filtering, its working without an error



My View to retrieve all the records in the database, without filtering.

def show_task(request):
tasks = Task.objects.all()
context = {'tasks': tasks}
#return render(request,"todoapp/show_task.html",{'tasks':tasks})
return render(request,"todoapp/show_task.html", context)

Antje Kazimiers

unread,
Feb 9, 2020, 8:25:51 AM2/9/20
to django...@googlegroups.com

Hi Chuck,

Status in your example is not a text field, but a Foreign Key, that means a reference to a record in another table Task_Status, you see that here:

    Status = models.ForeignKey(Task_Status, on_delete=models.CASCADE)

You need to filter on the attribute within Task_Status, which has the value 'In progress', probably the 'name' attribute:

tasks = Task.objects.filter(Status__name='In progress')

The error says it expects an id, which is the primary key of the Task_Status record the Task record is referring to.


Hope that helps,
Antje
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/a13041e8-1c37-4722-bf64-4faa75dd56ff%40googlegroups.com.

Farai M

unread,
Feb 9, 2020, 11:15:11 AM2/9/20
to django...@googlegroups.com
The status is a foreign ID u can use choices if the status is not dynamically changed by the users . Otherwise you will need to pass the selected status from fronted or hardcode it.

Chucky Mada Madamombe

unread,
Feb 9, 2020, 12:59:13 PM2/9/20
to django...@googlegroups.com
Thanks Farai!

Regards

Chuck G. Madamombe
NAM: +264 81 842 1284
RSA: +27 78 208 7034
Twitter: @chuckygari
Skype: chuckygari
Facebook: Chucky Mada Madamombe
LinkedIn: Chucknorris Garikayi Madamombe

Chucky Mada Madamombe

unread,
Feb 9, 2020, 12:59:14 PM2/9/20
to django...@googlegroups.com
Antje,

Thanks very much for your help!

Regards

Chuck G. Madamombe
NAM: +264 81 842 1284
RSA: +27 78 208 7034
Twitter: @chuckygari
Skype: chuckygari
Facebook: Chucky Mada Madamombe
LinkedIn: Chucknorris Garikayi Madamombe

BRAJESH KUMAR

unread,
Feb 9, 2020, 1:31:30 PM2/9/20
to django...@googlegroups.com

I have a local host website working with some page showing few details of one of many products with a data associated for usage like zoom car rental.

 

Facing issues while trying to add a counter and have it updated by day.

 

Eg: if I have a product a available 10 units for next 10 days and it gets booked for one day 3 units, the counter should update.

 

Need help for the same.

 

Thanks,

Brajesh

 

Sent from Mail for Windows 10

maninder singh Kumar

unread,
Feb 10, 2020, 2:59:44 AM2/10/20
to django...@googlegroups.com
have you tried "OneToOneField" ?
I am wondering about a many to one field to User
 
               
 


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages