我有 2 个模型:
class Tag(models.Model):
id = models.AutoField(primary_key=True)
name = models.CharField(max_length=255)
def __str__(self):
return self.name
class Question(models.Model):
name = models.CharField(max_length=255)
Tag_name = models.ManyToManyField(Tag)
def __str__(self):
return self.name
views.py
class QuestionList(APIView):
def get(self, request, tag_id):
res = Question.objects.filter(Tag_name=tag_id).prefetch_related('Tag_name').order_by('name')[:10]
print(res)
serializer = QuestionSerializers(res, many=True)
data = {}
return Response(serializer.data)
# return Response(data)
urls.py
urlpatterns = [
path('admin/', admin.site.urls),
path('tag=<int:tag_id>/', views.QuestionList.as_view()) //this needs to be edited
]
url.py 文件中发送 id 和 name 参数并获取数据的路径是什么
http://127.0.0.1:8000?tag=4&order_by=name
所以我收到所有关于标签 4 的问题并按名称排序?
慕虎7371278
一只萌萌小番薯
相关分类