Hi, everyone. Django relational model question.
class Actress(models, model):
Actress = models.CharField(max_length=30)
class Movielist(models.Model):
title = models.CharField(max_length=30)
cast = models.CharField(max_length=300)
Among the two models above, we want to make the action field and the cast field many-to-many.
However, the data of the cast field is stored in one column. (ex: Won Bin, Lee Na-young, Cho Jin-woong)
In this case, is it possible to map the data of the cast field in one column and the Actress field to many to many?
I wonder what to do if possible.
django python
Take a look at the documentation on Many-to-many relationship.
Configure the model like this.
from django.db import models
class Movie(models.Model):
title = models.CharField(max_length=30)
class Actress(models.Model):
title = models.CharField(max_length=100)
movies = models.ManyToManyField(Movie)
You can save the relationship as below.
>>> m1 = Publication (title='National Team 2')
>>> m1.save()
>>> m2 = Publication (title = 'cold')
>>> m2.save()
>>> m3 = Publication (title = 'Late Night FM')
>>> m3.save()
>>> a1 = Article (title = 'Suae')
>>> a1.save()
>>> a1.movies.add(m1)
>>> a1.movies.add(m2)
>>> a1.movies.add(m3)
a1.movies.all()
allows you to bring all of the actor's movies.
m1.actress_set.all()
will allow you to import all the actors in that movie.
© 2024 OneMinuteCode. All rights reserved.