如何列出特定问题的所有解决方案以及如何在 Django 中为它制作相应的 URL?
How can I list all the solutions to a particular question and how do I make the respective URL for it in Django?
我有两个模型,问题和解决方案。一个问题可以有很多解,但一个解只能有一个问题。
他们在这里:
models.py
class Question(models.Model):
question_title = models.CharField(max_length = 100)
question_description = models.TextField()
question_tags = models.TextField()
def __str__(self):
return self.question_title
class Solution(models.Model):
user_profile = models.ForeignKey(UserProfile, on_delete = models.SET_NULL)
question_id = models.ForeignKey(Question, on_delete = models.CASCADE, blank = False, null = True)
solution = models.TextField()
date_modified = models.DateTimeField(auto_now_add = True)
def __str__(self):
return "[SOL] " + self.question_id + "/" + self.user_profile
这是我的 views.py:
class QuestionList(generics.GenericAPIView, mixins.ListModelMixin, mixins.CreateModelMixin):
# Used to access all questions and adding one
queryset = Question.objects.all()
serializer_class = QuestionSerializer
def get(self, request):
return self.list(request)
def post(self, request):
return self.create(request)
class QuestionDetails(generics.GenericAPIView, mixins.RetrieveModelMixin, mixins.UpdateModelMixin,
mixins.DestroyModelMixin):
queryset = Question.objects.all()
serializer_class = QuestionSerializer
lookup_field = 'id'
def get_question(self, id):
try:
return Question.objects.get(id = id)
except Question.DoesNotExist:
raise Http404
def get(self, request, id):
question = self.get_question(id)
serializer = QuestionSerializer(question)
return Response(serializer.data, status.HTTP_200_OK)
def put(self, request, id):
question = self.get_question(id)
serializer = QuestionSerializer(question, data = request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status = status.HTTP_201_CREATED)
return Response(serializer.errors, status = status.HTTP_400_BAD_REQUEST)
def delete(self, request, id):
question = self.get_object(id)
question.delete()
return Response(status = status.HTTP_204_NO_CONTENT)
class SolutionList(generics.GenericAPIView, mixins.ListModelMixin,mixins.CreateModelMixin):
# Empty because I'm still incredibly confused
这是我的 urlpatterns
:
urlpatterns = [
path("questions/", QuestionList.as_view()),
path("questions/<int:id>/", QuestionDetails.as_view()),
]
第一个是访问问题列表,第二个是访问特定问题。我希望 URL 访问特定问题的解决方案列表,如下所示:
questions/question_id/solutions/
其中 question_id
是特定问题的 ID。我还希望 URL 访问特定问题的特定解决方案,如下所示:
questions/question_id/solutions/solution_id/
其中 solution_id
是特定解决方案的 ID。
我该怎么做?
感谢帮助。
我不使用 mixin,但这是适合你的功能..
def show_solution(request, q_id, s_id):
context = {
'q' : Question.objects.get(pk=q_id),
's' : Solution.objects.filter(question_id=question),
}
return render (request, 'template.html', context)
我有两个模型,问题和解决方案。一个问题可以有很多解,但一个解只能有一个问题。
他们在这里:
models.py
class Question(models.Model):
question_title = models.CharField(max_length = 100)
question_description = models.TextField()
question_tags = models.TextField()
def __str__(self):
return self.question_title
class Solution(models.Model):
user_profile = models.ForeignKey(UserProfile, on_delete = models.SET_NULL)
question_id = models.ForeignKey(Question, on_delete = models.CASCADE, blank = False, null = True)
solution = models.TextField()
date_modified = models.DateTimeField(auto_now_add = True)
def __str__(self):
return "[SOL] " + self.question_id + "/" + self.user_profile
这是我的 views.py:
class QuestionList(generics.GenericAPIView, mixins.ListModelMixin, mixins.CreateModelMixin):
# Used to access all questions and adding one
queryset = Question.objects.all()
serializer_class = QuestionSerializer
def get(self, request):
return self.list(request)
def post(self, request):
return self.create(request)
class QuestionDetails(generics.GenericAPIView, mixins.RetrieveModelMixin, mixins.UpdateModelMixin,
mixins.DestroyModelMixin):
queryset = Question.objects.all()
serializer_class = QuestionSerializer
lookup_field = 'id'
def get_question(self, id):
try:
return Question.objects.get(id = id)
except Question.DoesNotExist:
raise Http404
def get(self, request, id):
question = self.get_question(id)
serializer = QuestionSerializer(question)
return Response(serializer.data, status.HTTP_200_OK)
def put(self, request, id):
question = self.get_question(id)
serializer = QuestionSerializer(question, data = request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status = status.HTTP_201_CREATED)
return Response(serializer.errors, status = status.HTTP_400_BAD_REQUEST)
def delete(self, request, id):
question = self.get_object(id)
question.delete()
return Response(status = status.HTTP_204_NO_CONTENT)
class SolutionList(generics.GenericAPIView, mixins.ListModelMixin,mixins.CreateModelMixin):
# Empty because I'm still incredibly confused
这是我的 urlpatterns
:
urlpatterns = [
path("questions/", QuestionList.as_view()),
path("questions/<int:id>/", QuestionDetails.as_view()),
]
第一个是访问问题列表,第二个是访问特定问题。我希望 URL 访问特定问题的解决方案列表,如下所示:
questions/question_id/solutions/
其中 question_id
是特定问题的 ID。我还希望 URL 访问特定问题的特定解决方案,如下所示:
questions/question_id/solutions/solution_id/
其中 solution_id
是特定解决方案的 ID。
我该怎么做?
感谢帮助。
我不使用 mixin,但这是适合你的功能..
def show_solution(request, q_id, s_id):
context = {
'q' : Question.objects.get(pk=q_id),
's' : Solution.objects.filter(question_id=question),
}
return render (request, 'template.html', context)