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
On 2/9/20 3:31 AM, Chuck G. Madamombe wrote:
> 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)
>
> --
> 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 [email protected]
> <mailto:[email protected]>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/a13041e8-1c37-4722-bf64-4faa75dd56ff%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/a13041e8-1c37-4722-bf64-4faa75dd56ff%40googlegroups.com?utm_medium=email&utm_source=footer>.
--
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 [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/03679dec-cbd3-0890-4a10-afdff2336734%40gmail.com.