I have two classes linked with a ForeignKey. My first class is "Categories", with "ID" and "Name" properties. The second class is "Documents", with "ID", "Title" and "User" properties, the last one is linked with a category defined in the first class.
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. 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"}]
Form to add a new document:
Title: XXXX
Category:
Sports,
Music
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"}]