Hari
Hari

Reputation: 9

How to select multiple choices for a field in django?

Ihave this Actor model on the app called crew.

from django.db import models

# Create your models here.
class Parent(models.Model):
    # name=models.CharField(max_length=30,unique=True,default=None)
    name=models.CharField(max_length=30,unique=True,default=None)
    about=models.TextField(max_length=2000,blank=True,null=True)
    class Meta:
        abstract = True
    def __str__(self):
        return self.name
class Actor(Parent):
    slug=models.SlugField(max_length=50,unique=True)
    photo=models.ImageField(upload_to='crew/actor')

i want to add this model as a foreign key into the model called Movie which is on the app movie. and also want to select multiple actors on the fiels called starring

from django.db import models
from django.contrib.auth.models import User
from crew.models import *
from review_awards.models import *
# Create your models here.
class Movie(models.Model):
    name=models.CharField(max_length=100,unique=True,blank=True)
    slug=models.SlugField(max_length=100,unique=True,blank=True)
    year=models.CharField(max_length=5,blank=True)
    language=models.CharField(max_length=50,blank=True)
    poster=models.ImageField(upload_to='movies/poster')
    cover=models.ImageField(upload_to='movies/cover',null=True)
    duration=models.CharField(max_length=50,default=None)
    streaming=models.ForeignKey(Streaming,on_delete=models.CASCADE,blank=True,null=True)
    ott=models.TextField(max_length=50,blank=True,null=True)
    **starring**=models.ForeignKey(Actor,on_delete=models.CASCADE,related_name='actor',blank=True,null=True)
    director=models.ForeignKey(Director,on_delete=models.CASCADE,blank=True,null=True)
    cinematographer=models.ForeignKey(Cinematographer,on_delete=models.CASCADE,null=True,blank=True)
    writer=models.CharField(max_length=100,blank=True)
    based =models.CharField(max_length=100,blank=True,null=True)
    genre=models.CharField(max_length=100,blank=True)
    awards=models.ForeignKey(Award,on_delete=models.CASCADE,null=True,blank=True)
    critic_reviews=models.ForeignKey(Critic_Review,on_delete=models.CASCADE,null=True,blank=True)
    audience_reviews=models.ForeignKey(Audience_Review,on_delete=models.CASCADE,null=True,blank=True)
    rating=models.CharField(max_length=20,blank=True)
    certification=models.ForeignKey(Certification,on_delete=models.CASCADE,null=True,blank=True)
    synopsis=models.TextField(max_length=1000,blank=True)
    trailer=models.TextField(max_length=50,blank=True)
    def __str__(self):
        return self.name

How can i implement it?, currently iam able to select only one choice.

Upvotes: -1

Views: 101

Answers (1)

Omid Roshani
Omid Roshani

Reputation: 1183

To allow for multiple actors in the starring field of the Movie model, you can use a ManyToManyField instead of a ForeignKey.

from django.db import models
from django.contrib.auth.models import User
from crew.models import Actor  # Import the Actor model from the crew app
from review_awards.models import *

class Movie(models.Model):


    starring = models.ManyToManyField(Actor, related_name='movies', blank=True)  # Use ManyToManyField for multiple actors
    

    def __str__(self):
        return self.name

Run migrations (python manage.py makemigrations and python manage.py migrate) after making these changes to update your database schema.

When you want to add or remove actor you can do it like this:

# Assuming you have a movie object named 'movie' and an actor object named 'actor'

# Add an actor to the starring field
movie.starring.add(actor)

# Remove an actor from the starring field
movie.starring.remove(actor)

To show these details in the view you can have a view like this:

from django.shortcuts import render
from .models import Movie

def movie_detail(request, movie_id):
    movie = Movie.objects.get(pk=movie_id)
    return render(request, 'movie_detail.html', {'movie': movie})

and in this template you can show starring like this:

<h1>{{ movie.name }}</h1>

<h2>Starring:</h2>
<ul>
  {% for actor in movie.starring.all %}
    <li>{{ actor.name }}</li>
  {% endfor %}
</ul>

Upvotes: 0

Related Questions