ORM๐น: ์ฅ๊ณ ManyToManyField
./blog/models.py
from django.db import models
class Blog(models.Model):
name = models.CharField(max_length=100)
tagline = models.TextField()
def __str__(self):
return self.name
class Author(models.Model):
name = models.CharField(max_length=200)
email = models.EmailField()
def __str__(self):
return self.name
class Entry(models.Model):
blog = models.ForeignKey(Blog, on_delete=models.CASCADE)
headline = models.CharField(max_length=255)
body_text = models.TextField()
pub_date = models.DateField()
mod_date = models.DateField()
authors = models.ManyToManyField(Author)
number_of_comments = models.IntegerField()
number_of_pingbacks = models.IntegerField()
rating = models.IntegerField()
def __str__(self):
return self.headline
์์ ๋ชจ๋ธ์ migrateํ๋ฉด DB์๋ ์ด๋ค ํ
์ด๋ธ๋ค์ด ์๊ธธ๊น?
- blog_blog
- blog_author
- blog_entry
๊ทธ๋ฆฌ๊ณ Entry์ ManyToManyField ์ค์ ์ ์๊ธด!
- blog_entry_authors
- blog_blog
CREATE TABLE "blog_blog" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "name" varchar(100) NOT NULL, "tagline" text NOT NULL)- blog_author
CREATE TABLE "blog_author" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "name" varchar(200) NOT NULL, "email" varchar(254) NOT NULL)- blog_entry
CREATE TABLE "blog_entry" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT,
"headline" varchar(255) NOT NULL,
"body_text" text NOT NULL,
"pub_date" date NOT NULL, "mod_date" date NOT NULL,
"number_of_comments" integer NOT NULL, "number_of_pingbacks" integer NOT NULL, "rating" integer NOT NULL,
"blog_id" integer NOT NULL REFERENCES "blog_blog" ("id") DEFERRABLE INITIALLY DEFERRED)- blog_entry_authors
CREATE TABLE "blog_entry_authors" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT,
"entry_id" integer NOT NULL REFERENCES "blog_entry" ("id") DEFERRABLE INITIALLY DEFERRED,
"author_id" integer NOT NULL REFERENCES "blog_author" ("id") DEFERRABLE INITIALLY DEFERRED)
ManyToManyField์ ํด๋น ํ๋๋ก ์ฐ๊ฒฐ๋ 2๊ฐ์ ํ ์ด๋ธ์ ์ํ ์ค๊ฐ ํ ์ด๋ธ์ ๋ง๋ค์ด ์ค๋น!
- Entryํ
์ด๋ธ์์ authors์ ManyToManyField๋ฅผ ํตํด์ author๋ฅผ ์ฐ๊ฒฐํด์ค!
- ๊ทธ๋ฌ๋๋ "blog_entry_authors"๋ผ๋ ์ค๊ฐ ํ
์ด๋ธ ํ์! ์ง !
- ์์ฑ๋๋ ํ ์ด๋ธ์ ์ข ๋ด๋ง๋๋ก ์ปค์คํฐ๋ง์ด์งํ๊ณ ์ถ๋ค๋ฉด "through"๋ฅผ ์๋๋น!
ManyToManyField์ ๋ฐ์ดํฐ ์ง์ด๋ฃ๋ ๋ฐฉ๋ฒ์ ๋ฐ๋ก add ๋ฉ์๋!
e = Entry.objects.get(pk=1)
b = Blog.objects.get(pk=1)
e.blog = b
e.save()
bob = Author.objects.create(name="Bob")
e.authors.add(bob)
add ๋ฉ์๋๋ ManyToManyField๋ก ์ง์ ๋ ์์ฑ์๋ง ๋ํ๋๋ ๋ฉ์๋์ด๋น!
ManyToManyField.through_fields
๊ธฐํ
Reference
- https://medium.com/@dlarkqrl4966/%EB%8C%80%EA%B7%9C%EB%AA%A8-django-%EC%95%B1-%EA%B5%AC%EC%B6%95%EC%9D%84%EC%9C%84%ED%95%9C-%ED%8C%81-cdd2138afec1
- https://docs.djangoproject.com/en/3.1/topics/db/queries/
- https://docs.djangoproject.com/en/3.1/ref/models/fields/#manytomanyfield
- https://docs.djangoproject.com/en/3.1/ref/models/fields/#django.db.models.ManyToManyField.through_fields
Author And Source
์ด ๋ฌธ์ ์ ๊ดํ์ฌ(ORM๐น: ์ฅ๊ณ ManyToManyField), ์ฐ๋ฆฌ๋ ์ด๊ณณ์์ ๋ ๋ง์ ์๋ฃ๋ฅผ ๋ฐ๊ฒฌํ๊ณ ๋งํฌ๋ฅผ ํด๋ฆญํ์ฌ ๋ณด์๋ค https://velog.io/@muchogusto/์ฅ๊ณ -ManyToManyField์ ์ ๊ท์: ์์์ ์ ๋ณด๊ฐ ์์์ URL์ ํฌํจ๋์ด ์์ผ๋ฉฐ ์ ์๊ถ์ ์์์ ์์ ์ ๋๋ค.
์ฐ์ํ ๊ฐ๋ฐ์ ์ฝํ ์ธ ๋ฐ๊ฒฌ์ ์ ๋ (Collection and Share based on the CC Protocol.)
์ข์ ์นํ์ด์ง ์ฆ๊ฒจ์ฐพ๊ธฐ
๊ฐ๋ฐ์ ์ฐ์ ์ฌ์ดํธ ์์ง
๊ฐ๋ฐ์๊ฐ ์์์ผ ํ ํ์ ์ฌ์ดํธ 100์ ์ถ์ฒ ์ฐ๋ฆฌ๋ ๋น์ ์ ์ํด 100๊ฐ์ ์์ฃผ ์ฌ์ฉํ๋ ๊ฐ๋ฐ์ ํ์ต ์ฌ์ดํธ๋ฅผ ์ ๋ฆฌํ์ต๋๋ค