Hi, everyone. I'm making a web app that shows a list of movies with actresses. So I downloaded the data through scrap and saved it in DB.
Below is the Django model.
class MovieList(models.Model):
release_date = models.CharField(max_length=30)
running_time = models.CharField(max_length=10)
name = models.CharField(max_length=200)
company = models.CharField(max_length=30)
number = models.CharField(max_length=30)
class Actressimages(models.Model):
name = models.CharField(null=True)
image_urls = models.CharField(max_length=200)
images = models.TextField()
image_paths = models.TextField(null=True)
I'm planning to make a list of actresses and show them movies It's blocked from here.
So, I modified the model field as below.
class MovieList(models.Model):
release_date = models.CharField(max_length=30)
running_time = models.CharField(max_length=10)
name = models.CharField(max_length=200)
company = models.CharField(max_length=30)
number = models.CharField(max_length=30)
class Actressimages(models.Model):
name = models.ManyToMany(MovieList)
image_urls = models.CharField(max_length=200)
images = models.TextField()
image_paths = models.TextField(null=True)
I think we can designate the name of the Actressimages class as many to many as above After doing it like that, I don't know how to match the legacy data to the name variable.
questions,
How do I put legacy data into a model of many-to-many relationships (?)? Or how can I display the data stored in the above two models in the template as I want (sort by actress name and click on the name)?
django
In Django, the model writes the surrogate key as the primary key by default. It's not even defined in the model, but the field "id" already exists, so you can think of it as organizing the relationship.
Therefore, I think the following definition will lead to more.
class MovieList(models.Model):
release_date = models.CharField(max_length=30)
running_time = models.CharField(max_length=10)
name = models.CharField(max_length=200)
company = models.CharField(max_length=30)
number = models.CharField(max_length=30)
class Actressimages(models.Model):
name = models.CharField(null=True)
image_urls = models.CharField(max_length=200)
images = models.TextField()
image_paths = models.TextField(null=True)
movies = models.ManyToMany(MovieList)
See the following example to save the N:N relationship.
First of all, when there are movies like this,
>>> m1 = new Movie List (name='Film Title', release_date=...)
>>> m1.save()
>>> m2 = new MovieList (name='other movie', release_date=...)
>>> m2.save()
I think you can do the following movies that actors A and A participated in.
>>> act1 = new Actressimages (name="Mr. A", images='',...)
>>> act1.save()
Please save it first as above.
>>> act1.movies.add(m1,m2)
Or
>>> act1.movies.add(m1)
>>> act1.movies.add(m2)
This saves the relationship.
Next, the list of movies that each actor participated in can be as follows.
>>> act1.movies.all()
Note: Online documentation
If it is a collection of movie information that is intended to be expressed in MoveList, I think it would be better to name the object in Movie rather than MovieList. SMT recommends that you specify a model name for one record. In the same vein, it's more likely that Actress or ActressImages will be produced than Actressimages.
It's a part that has nothing to do with logic, so you can do whatever you want. :)
© 2024 OneMinuteCode. All rights reserved.