Django中model的get()查询无法返回对象,请求技术协助
Hey there! Let's troubleshoot that frustrating error you're seeing when accessing your topic page. This is a common pitfall when building forum-like apps (I’m assuming you’re using Django given the URL structure and error message), so let’s walk through the most likely issues and fixes.
First, Let's Confirm the Core Setup
I’ll assume your models follow a standard forum structure like this:
# models.py from django.db import models class Board(models.Model): name = models.CharField(max_length=100) # Add other fields like description if needed class Topic(models.Model): board = models.ForeignKey(Board, on_delete=models.CASCADE, related_name="topics") title = models.CharField(max_length=200) # Add other fields like creator, created_at, etc.
And your URL pattern is set up to pass both board_id and topic_id:
# urls.py from django.urls import path from . import views urlpatterns = [ # Other paths... path("boards/<int:board_id>/topics/<int:topic_id>/", views.topic_detail, name="topic_detail"), ]
The Most Likely Culprit: Your View's Filter Logic
The error almost always happens because your view is only filtering by topic_id, not verifying that the topic actually belongs to the specified board_id.
Wrong Approach (Causes the Error)
If your view looks like this, it’s missing the critical board association check:
# views.py (incorrect) from django.shortcuts import get_object_or_404, render from .models import Topic def topic_detail(request, board_id, topic_id): # Only checks topic ID, not which board it's associated with topic = get_object_or_404(Topic, id=topic_id) return render(request, "topic_detail.html", {"topic": topic})
For example: If there’s a Topic with id=1 that belongs to Board with id=2, accessing /boards/1/topics/1/ will fail—even though the topic exists, it’s not in the requested board.
Correct Approach
Update your view to filter by both the topic’s ID and its linked board ID:
# views.py (correct) from django.shortcuts import get_object_or_404, render from .models import Topic def topic_detail(request, board_id, topic_id): # Ensure the topic exists AND belongs to the specified board topic = get_object_or_404(Topic, id=topic_id, board_id=board_id) # Alternatively, use `board__id=board_id` (same effect) return render(request, "topic_detail.html", {"topic": topic})
Other Possible Checks
If the above fix doesn’t resolve the error, verify these two things:
Check Your Database Data
Make sure there’s actually aBoardwithid=1and aTopicwithid=1linked to that board. You can confirm this via Django Admin or the Django shell:# Run `python manage.py shell` in your terminal from your_app_name.models import Board, Topic # Check if the board exists print(Board.objects.filter(id=1).exists()) # Should return True # Check if the topic exists and is linked to the board print(Topic.objects.filter(id=1, board_id=1).exists()) # Should return TrueVerify URL/View Parameter Names
Double-check that parameter names in your URL pattern match what your view expects. For example, if your URL uses<int:board_pk>instead of<int:board_id>, your view function must acceptboard_pkas an argument (notboard_id). Mismatched names will pass incorrect values to your filter.
Final Notes
By adding the board_id filter to your get_object_or_404() call, you’re not just fixing the error—you’re also making your app more secure, preventing users from accessing topics in unintended boards via manipulated URLs.
内容的提问来源于stack exchange,提问作者RedTears

