简体   繁体   English

从Django-rest检索数据以表格形式显示

[英]Retrieve data from django-rest to show in a form

I have two classes linked with a ForeignKey. 我有两个与ForeignKey链接的类。 My first class is "Categories", with "ID" and "Name" properties. 我的第一堂课是“类别”,具有“ ID”和“名称”属性。 The second class is "Documents", with "ID", "Title" and "User" properties, the last one is linked with a category defined in the first class. 第二类是“文档”,具有“ ID”,“标题”和“用户”属性,最后一个与第一类中定义的类别链接。

I'm developing the front-end based on Vue, so I want to know how to retrieve data from django-rest to show it in a form. 我正在开发基于Vue的前端,因此我想知道如何从django-rest检索数据以表格形式显示。 For example, when a user adds a new doc, he must choose between all category options. 例如,当用户添加新文档时,他必须在所有类别选项之间进行选择。 Note that categories will be common to all documents. 请注意,类别对于所有文档都是通用的。

Example: Categories = [{"ID": 0, "Name "Sports"}, {"ID": 1, "Name": "Music"}] 例如:Categories = [{“ ID”:0,“ Name” Sports“},{” ID“:1,” Name“:” Music“}]

Form to add a new document: 表单以添加新文档:

Title: XXXX 标题:XXXX

Category: 类别:
Sports, 体育,
Music 音乐

models.py models.py


from django.db import models

class Category(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=200)

class Documents(models.Model):
    id = models.AutoField(primary_key=True)
    title= models.CharField(max_length=200)
    category = models.ForeignKey(Category, on_delete=models.CASCADE)

I think that if I get a answer like below it will resolve my problem. 我认为,如果我得到如下所示的答案,它将解决我的问题。

["docs": 
     [{"ID": 1, "Title": "Doc1", "Category": Sports}, 
     {"ID": 2, "Title": "Doc2", "Category": Music}], 
 "categories": 
     [{"ID": 0, "Name: "Sports"}, 
     {"ID": 1, "Name": "Music"}]

不知道Vue,但是您可以通过GET请求获取所有类别,然后使用这些类别作为选项填充下拉菜单,并获得提交时的下拉菜单值。

You could make a separated request to get all the category before you create the form. 您可以在创建表单之前单独请求获取所有类别。

To do this, you need to create a CategorySerializer and a ListCategoryProvider 为此,您需要创建一个CategorySerializer和一个ListCategoryProvider

class CategorySerializer(serializers.ModelSerializer):

    class Meta:
        model = Category
        fields = {'name'}

class ListCategoryProvider(generics.ListAPIView):
    serializer_class = CategorySerializer

    def get_queryset(self):
        queryset = Category.objects.all()
        return queryset

Then you need to parse the JSON containing all the categories names, to display them in your form 然后,您需要解析包含所有类别名称的JSON,以在表单中显示它们

1) write a serializer in the Django rest for category and document. 1)在Django rest中为类别和文档编写一个序列化器。 Please go through the link for serializer https://www.django-rest-framework.org/api-guide/serializers/ 请通过序列化器链接https://www.django-rest-framework.org/api-guide/serializers/

2) write a form in html where you can insert the document. 2)用html编写表格,您可以在其中插入文档。 In the javascript(vue), you can call ajax request(get request) for the category and display the category's in select options. 在javascript(vue)中,您可以调用该类别的ajax request(获取请求),并在选择选项中显示该类别的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM