[Django]ManyToManyField(feat.models.py)

jiminnoteยท2022๋…„ 6์›” 8์ผ
0

๐ŸŒฑDjango

๋ชฉ๋ก ๋ณด๊ธฐ
8/13
post-thumbnail

๐Ÿ“ManyToManyField

๐ŸŒฑmodels.py

from django.db import models

# Create your models here.
class Actor(models.Model):
    first_name = models.CharField(max_length=45)
    last_name = models.CharField(max_length=45)
    date_of_birth=models.DateField()
    movies=models.ManyToManyField('Movie')
    #๊ด€๊ณ„๋ช…์„ ์ง์ ‘ ์ง€์–ด์ฃผ๊ณ ์‹ถ์œผ๋ฉด ('Movie',db_table="actors_movies")
    #ํด๋ž˜์Šค๋ช…์†Œ๋ฌธ์ž_set ๊ฐ€ ์•„๋‹Œ ์ƒˆ๋กœ์šด ์ด๋ฆ„์ง€์ •์›ํ•˜๋ฉด ('Movie',db_table="actors_movies",related_name='movie')
    class Meta:
        db_table='actors'
    def __str__(self):
        return self.first_name + self.last_name
        #__str__ ํ•จ์ˆ˜:๊ฐ์ฒด๋ฅผ ๋ฌธ์ž์—ด๋กœ ํ‘œํ˜„ํ•œ ๊ฒƒ์„ ๋ฐ˜ํ™˜ ex.<Actor:์ด์ง€์€ >

class Movie(models.Model):
    title = models.CharField(max_length=45)
    release_date = models.DateField()
    running_time=models.IntegerField()
    
    
    class Meta:
        db_table='movies'
        
    def __str__(self):
        return self.title
        #__str__ ํ•จ์ˆ˜:๊ฐ์ฒด๋ฅผ ๋ฌธ์ž์—ด๋กœ ํ‘œํ˜„ํ•œ ๊ฒƒ์„ ๋ฐ˜ํ™˜ ex.<Movie: ๋ธŒ๋กœ์ปค>

๋‚ด๊ฐ€ ๋งŒ๋“  ํ…Œ์ด๋ธ”์€ actors์™€ movies 2๊ฐœ์ง€๋งŒ
ManyToManyField()์„ ์‚ฌ์šฉํ•˜์—ฌ actors_movies ๋ผ๋Š” ํ…Œ์ด๋ธ”์ด ์ž๋™์„ ์ƒ์„ฑ๋˜์—ˆ๋‹ค.

actors_moviesํ…Œ์ด๋ธ”์€ id,actor_id,movie_id๋กœ ๊ตฌ์„ฑ๋˜์–ด์žˆ๋‹ค.

๋ฐ์ดํ„ฐ ๋„ฃ๊ธฐ

>>> from movies.models import Actor,Movie
>>> Movie.objects.create(title='๋ธŒ๋กœ์ปค',release_date='2022-06-08',running_time=129)
<Movie: ๋ธŒ๋กœ์ปค> 
>>> Movie.objects.create(title='๊ธฐ์ƒ์ถฉ',release_date='2019-05-30',running_time=132)
<Movie: ๊ธฐ์ƒ์ถฉ>
>>> Movie.objects.create(title='๋ฒ”์ฃ„๋„์‹œ2',release_date='2022-05-18',running_time=106)
<Movie: ๋ฒ”์ฃ„๋„์‹œ2>
>>> Movie.objects.create(title='๋ถ€๋ผ๋”',release_date='2017-11-02',running_time=102)
<Movie: ๋ถ€๋ผ๋”>
>>> Actor.objects.create(first_name='์ด',last_name='์ง€์€',date_of_birth='1993-05-16')
<Actor: ์ด์ง€์€>
>>> Actor.objects.create(first_name='๋งˆ',last_name='๋™์„',date_of_birth='1971-03-01')
<Actor: ๋งˆ๋™์„>
>>> Actor.objects.create(first_name='์†ก',last_name='๊ฐ•ํ˜ธ',date_of_birth='1967-02-25')
<Actor: ์†ก๊ฐ•ํ˜ธ>

ManyToManyField

์ฒซ๋ฒˆ์งธ ๋ฐฉ๋ฒ•

>>> Actor.objects.get(id=1).movies.add(Movie.objects.get(id=1))
>>> Actor.objects.get(id=3).movies.add(Movie.objects.get(id=1))
>>> Actor.objects.get(id=3).movies.add(Movie.objects.get(id=2))
>>> Actor.objects.get(id=2).movies.add(Movie.objects.get(id=3))
>>> Actor.objects.get(id=2).movies.add(Movie.objects.get(id=4))

๐Ÿ‘‰ actor_id=1์— ์—ฐ๊ฒฐ๋  ์˜ํ™”๋ชฉ๋ก์— movie_id=1 ์ถ”๊ฐ€
๐Ÿ‘‰ actor_id=3์— ์—ฐ๊ฒฐ๋  ์˜ํ™”๋ชฉ๋ก์— movie_id=1 ์ถ”๊ฐ€
.
.
.
๋‘๋ฒˆ์งธ ๋ฐฉ๋ฒ•

>>> a1 = Actor.objects.get(id=1)
>>> a1.save() #saveํ•ด์ค˜์•ผํ•จ! ๊ผญ!
>>> a2 = Actor.objects.get(id=2)
>>> a2.save()
>>> a3 = Actor.objects.get(id=3)
>>> a3.save()
>>> m1 = Movie.objects.get(id=1)
>>> m1.save()
>>> m2 = Movie.objects.get(id=2)
>>> m2.save()
>>> m3 = Movie.objects.get(id=3)
>>> m3.save()
>>> m4 = Movie.objects.get(id=4)
>>> m4.save() 
>>> a1.movie.add(m1)
>>> a2.movie.add(m3,m4)
>>> a3.movie.add(m1,m2)
#Actorํด๋ž˜์Šค์— ManyToManyField()๊ฐ€ ์žˆ์œผ๋‹ˆ actor์— movie๋ฅผ ๋„ฃ๊ธฐ

profile
Hello. I'm jimin:)

0๊ฐœ์˜ ๋Œ“๊ธ€