can any one tell how can i get an appropriate output only using a foreign key in models

58 views Asked by At

this is my models.py file
models.py

from django.db import models
from django.utils import timezone

class Movielist(models.Model) :
    Title = models.CharField(max_length=1000)
    Description = models.TextField(blank=True)
    ReleaseDate = models.DateTimeField(verbose_name='Release Date', blank=True)
    Upvote = models.IntegerField(default=0)
    Downvote = models.IntegerField(default=0)
    
    def __str__(self):
        return self.Title

class Actorlist(models.Model):
    Name = models.CharField(max_length=1000)
    DateofBirth = models.DateTimeField(verbose_name='Date of Birth',blank=True)

    def __str__(self):
        return self.Name

class ActorInMovie(models.Model):
    Movie = models.ForeignKey(Movielist, default=1, on_delete=models.CASCADE, blank=True, related_name='ActorInMovie')
    Actor = models.ForeignKey(Actorlist, default=1, on_delete=models.CASCADE, blank=True,related_name='ActorsMovie')
    
    def __str__(self):
        return self.Movie.Title

this is my views.py file
views.py

@api_view(['GET'])
def apiOverview(request):
    api_urls = {
        'List':'/task-list/',
        'Detail View':'/task-detail/<str:pk>/',
        'Create':'/task-create/',
        'Update':'/task-update/<str:pk>/',
        'Delete':'/task-delete/<str:pk>/',
    }
    return Response(api_urls)

@api_view(['GET'])
def MovieName(request):
    MovieName = Movielist.objects.prefetch_related('ActorInMovie')
    serializer = MovieListSerializer(MovieName, many=True)
    return Response(serializer.data)

this is my serializers.py file serializers.py

from rest_framework import serializers
from .models import *

class MovieListSerializer(serializers.ModelSerializer):
    class Meta:
        model = Movielist
        fields = "__all__"

As output i am getting this as below

[
    {
        "id": 1,
        "Title": "KGF Chapter 1",
        "Description": "This is very thrilling and suspenseful movie",
        "ReleaseDate": "2022-04-18T16:22:03Z",
        "Upvote": 5,
        "Downvote": 2
    },
    {
        "id": 2,
        "Title": "RRR",
        "Description": "This is a south Indian movie",
        "ReleaseDate": "2022-04-19T06:46:54Z",
        "Upvote": 2,
        "Downvote": 3
    },
    {
        "id": 3,
        "Title": "Doctor Strange",
        "Description": "this movie is a part of marvel cinematic universe.",
        "ReleaseDate": "2022-04-22T07:19:07Z",
        "Upvote": 12,
        "Downvote": 7
    }
]

can anyone tell me how do i get a output like this

[
    {
        "id": 1,
        "Title": "KGF Chapter 1",
        "Description": "This is very thrilling and suspenseful movie",
        "ReleaseDate": "2022-04-18T16:22:03Z",
        "Actor": {yxz, yzx, xyz}
        "Upvote": 5,
        "Downvote": 2
    },
    {
        "id": 2,
        "Title": "RRR",
        "Description": "This is a south Indian movie",
        "ReleaseDate": "2022-04-19T06:46:54Z",
        "Actor": {yxz, yzx, xyz}
        "Upvote": 2,
        "Downvote": 3
    },
    {
        "id": 3,
        "Title": "Doctor Strange",
        "Description": "this movie is a part of marvel cinematic universe.",
        "ReleaseDate": "2022-04-22T07:19:07Z",
        "Actor": {yxz, yzx, xyz}
        "Upvote": 12,
        "Downvote": 7
    }
]

please help me to resolve this error.. i have to further link this angular so please help me to me find an appropriate output

1

There are 1 answers

1
Janik Spies On

First I would use a ManyToMany relationship between the movies and the actors. This would look something like this:

models.py:

class Actorlist(models.Model):
    Name = models.CharField(max_length=1000)
    DateofBirth = models.DateTimeField(verbose_name='Date of Birth',blank=True)

    def __str__(self):
        return self.Name


class Movielist(models.Model) :
    Title = models.CharField(max_length=1000)
    Description = models.TextField(blank=True)
    ReleaseDate = models.DateTimeField(verbose_name='Release Date', blank=True)
    Upvote = models.IntegerField(default=0)
    Downvote = models.IntegerField(default=0)
    Actors = models.ManyToManyField(Actorlist)
    
    def __str__(self):
        return self.Title


After that you should see the actors in your output. I would also recommend reading the relationship documentation from django-rest-framework:

https://www.django-rest-framework.org/api-guide/relations/