๐Ÿชฃ Many to Many ๊ด€๊ณ„ ํ…Œ์ด๋ธ”

12510 ๋‹จ์–ด ์žฅ๊ณ ์žฅ๊ณ 

์žฅ๊ณ  Many to Many ๊ด€๊ณ„ํ…Œ์ด๋ธ”์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด์ž.

๐Ÿš€ Many To Many

  • ์žฅ๊ณ ์—์„œ๋Š” Mant To many ์˜ต์…˜์„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ ๋‹จ์ˆœํžˆ ๊ด€๊ณ„ ํ…Œ์ด๋ธ”์„ ๋‘๋Š” ๊ฒƒ๊ณผ Many To Many์˜ต์…˜์„ ์ง€์ •ํ–ˆ์„ ๋•Œ ์ฐจ์ด์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

  • ์˜ˆ์‹œ ๋ฐ์ดํ„ฐ๋Š” ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

    ์˜ํ™”์ •๋ณด๋ฅผ ๊ฐ–๊ณ  ์žˆ๋Š” ์˜ํ™” ํ…Œ์ด๋ธ”๊ณผ ๋ฐฐ์šฐ ์ •๋ณด๋ฅผ ๋‹ด๊ณ  ์žˆ๋Š” ๋ฐฐ์šฐํ…Œ์ด๋ธ” ๊ทธ๋ฆฌ๊ณ  ์ด ๋‘˜์„ ์ด์–ด์ฃผ๋Š”์ค‘๊ณ„ํ…Œ์ด๋ธ” ์ž…๋‹ˆ๋‹ค.

๐Ÿค ์ค‘๊ณ„ ํ…Œ์ด๋ธ” ์ƒ์„ฑ

  • Many to Many ํ•„๋“œ๋ฅผ ์ง€์ •ํ•˜์ง€ ์•Š๊ณ  ์ค‘๊ณ„ ํ…Œ์ด๋ธ”๋งŒ ๋งŒ๋“ค์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.
class Actor(models.Model):
    name = models.CharField(max_length=45)
    
class Movie(models.Model):
    title = models.CharField(max_length=256)
                              

class ActorMovie(models.Model):
    actor = models.ForeignKey(Actor, on_delete=models.CASCADE)
    movie = models.ForeignKey(Movie, on_delete=models.CASCADE)

๊ฐ€๋…์„ฑ์„ ์œ„ํ•ด์„œ ์ปฌ๋Ÿผ์˜ ์ˆ˜๋Š” ์ตœ๋Œ€ํ•œ ์ค„์˜€์Šต๋‹ˆ๋‹ค.

๋ฐ์ดํ„ฐ๋ฅผ ๋„ฃ์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

  • ์˜ํ™” ์ƒ์„ฑ
m1 = Movie.objects.create(title="ํ…Œ๋„ท")
m2 = Movie.objects.create(title="๋ธ”๋ ˆ์ด๋“œ๋Ÿฌ๋„ˆ2049")
  • ๋ฐฐ์šฐ ์ƒ์„ฑ
a1 = Actor.objects.create(name="์กด ๋ฐ์ด๋น„๋“œ ์›Œ์‹ฑํ„ด")
a2 = Actor.objects.create(name="๋ผ์ด์–ธ ๊ณ ์Šฌ๋ง")
  • ๊ด€๊ณ„ ํ…Œ์ด๋ธ” ๋ฐ์ดํ„ฐ ๋„ฃ๊ธฐ
ActorMovie.objects.create(actor=a1, movie=m1)
ActorMovie.objects.create(actor=a2, movie=m2)
  • 1๋ฒˆ ๋ฐฐ์šฐ ์กด ๋ฐ์ด๋น„๋“œ ์›Œ์‹ฑํ„ด์˜ ์ถœ์—ฐ ์˜ํ™”๋ฅผ ์กฐํšŒํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.
a1.actormovie_set.all()

for actormovie in a1.actormovie_set.all():
    print(actormovie.movie.title)

์˜ํ™”, ๋ฐฐ์šฐ ๊ด€๊ณ„ ํ…Œ์ด๋ธ”์„ ์—ญ์ฐธ์กฐํ•ด์„œ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ด์„œ ์š”์†Œ๋ฅผ ํ•˜๋‚˜ ๋นผ์„œ .movie.title๋กœ ์ ‘๊ทผํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ข€ ๋ฒˆ๊ฑฐ๋กญ๋‹ค๋Š” ๋Š๋‚Œ์ด ๋“ญ๋‹ˆ๋‹ค.

๐Ÿชฃ Many to Many ์„ค์ •

  • ์ด๋ฒˆ์—๋Š” ์˜ํ™” ํ…Œ์ด๋ธ”์— actor์นผ๋Ÿผ์„ ๋„ฃ๊ณ  many to many๋ฅผ ์ง€์ •ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.
class Actor(models.Model):
    name = models.CharField(max_length=45)
    
class Movie(models.Model):
    title = models.CharField(max_length=256)
    actor = models.ManyToManyField(Actor,
                                   through='ActorMovie',
                                   related_name='movies')
                            
class ActorMovie(models.Model):
    actor = models.ForeignKey(Actor, on_delete=models.CASCADE)
    movie = models.ForeignKey(Movie, on_delete=models.CASCADE)
  • through ์˜ต์…˜์€ ๊ด€๊ณ„ํ…Œ์ด๋ธ”์„ ์ง€์ •ํ•˜๋Š” ์˜ต์…˜
  • related_name์€ ์—ญ์ฐธ์กฐ๋ฅผ ํ•˜๊ธฐ์œ„ํ•œ ์ด๋ฆ„ ์„ค์ •

์ด์ œ ์˜ํ™” ๋‹ค์‹œ ์กด ๋ฐ์ด๋น„๋“œ ์›Œ์‹ฑํ„ด๋ฐฐ์šฐ์˜ ์˜ํ™”๋ฅผ ์กฐํšŒํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

a1.movies.all()
for movie in a1.movies.all():
	print(movie.title)
>>> "ํ…Œ๋„ท"

๊ด€๊ณ„ ํ…Œ์ด๋ธ”์„ ์ƒ์„ฑํ•œ ๊ฒฝ์šฐ๋ณด๋‹ค ๋ฉ”์†Œ๋“œ ํ•œ๊ฐœ๋ฅผ ๋œ ์“ธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. .movie.title -> .title

์ข€ ๋” ์ง๊ด€์ ์ด๊ฒŒ ๊ด€๊ณ„ํ•œ ํ…Œ์ด๋ธ”์„ ์กฐํšŒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๐Ÿงช ์ค‘๊ณ„ ํ…Œ์ด๋ธ” ์—†์• ๊ธฐ

  • ์ค‘๊ณ„ ํ…Œ์ด๋ธ”์„ ์—†์• ๊ณ  Many to Many์˜ต์…˜์„ ์ง€์ •ํ•ด๋„ ๋ฉ๋‹ˆ๋‹ค.
class Actor(models.Model):
    name = models.CharField(max_length=45)
    
class Movie(models.Model):
    title = models.CharField(max_length=256)
    actor = models.ManyToManyField(Actor,
                                   related_name='movies')

์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ์•„๊นŒ์™€ ๊ฐ™์ด ๋ฐ์ดํ„ฐ๋ฅผ ์กฐํšŒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

a1 = Actor.objects.create(name="์กด ๋ฐ์ด๋น„๋“œ ์›Œ์‹ฑํ„ด")
a1.movies.all()
for movie in a1.movies.all():
	print(movie.title)
>>> "ํ…Œ๋„ท"

๐Ÿค” ์ค‘๊ณ„ ํ…Œ์ด๋ธ”์€ ์™œ๋‘˜๊นŒ?

  • ๊ทธ๋ ‡๋‹ค๋ฉด ์˜๋ฌธ์ด ํ•˜๋‚˜ ์ƒ๊น๋‹ˆ๋‹ค. ๋ญํ•˜๋Ÿฌ ์ค‘๊ณ„ํ…Œ์ด๋ธ”์„ ๋‘๋Š”๊ฐ€?

์ค‘๊ณ„ ํ…Œ์ด๋ธ”์— ๊ด€๊ณ„ํ•˜๋Š” ํ…Œ์ด๋ธ” foreignkey์™ธ์— ๋‹ค๋ฅธํ•ญ๋ชฉ์„ ์ถ”๊ฐ€ํ•˜๊ณ ์ž ํ•  ๋•Œ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.

์ค‘๊ณ„ํ…Œ์ด๋ธ”์„ ์•ˆ ๋งŒ๋“ค๊ณ  Many to Many์˜ต์…˜์„ ์ง€์ •ํ•˜๋ฉด ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์ž๋™์œผ๋กœ ๊ด€๊ณ„ํ…Œ์ด๋ธ”์ด ์ƒ์„ฑ๋˜๋Š”๋ฐ ์ด ํ…Œ์ด๋ธ”์— ์ปฌ๋Ÿผ์„ ์ถ”๊ฐ€ํ•˜๊ณ ์ž ํ•˜๋ฉด ์ง์ ‘ ์žฅ๊ณ ์—์„œ ๋งŒ๋“ค์–ด์•ผ ์ถ”๊ฐ€ํ•˜๊ณ ์ž ํ•˜๋Š” ์ปฌ๋Ÿผ์ด ๋ฐ˜์˜๋ฉ๋‹ˆ๋‹ค.

์ข‹์€ ์›นํŽ˜์ด์ง€ ์ฆ๊ฒจ์ฐพ๊ธฐ