class Account(models.Model):
name = models.CharField(max_length=50)
nickname = models.CharField(max_length=50)
phone_number = models.CharField(max_length=50, unique=True)
password = models.CharField(max_length=200)
email = models.EmailField(max_length=200, unique=True)
class Meta:
db_table = "accounts"
class Posting(models.Model):
user = models.ForeignKey(Account, on_delete=models.CASCADE)
post = models.CharField(max_length=50)
content = models.CharField(max_length=50)
created_at = models.DateTimeField(auto_now_add=True)
# updated_at = models.DateTimeField(auto_now=True)
img_url = models.URLField(max_length=200)
class Meta:
db_table = "postings"
Posting์ Account๋ฅผ ์ ์ฐธ์กฐํ๋ฏ๋ก ์์ฑ์ผ๋ก ์ ๊ทผ์ด ๊ฐ๋ฅํ๋ฐ, Account ๊ฐ์ฒด๋ Posting ๊ฐ์ฒด๋ฅผ ์ญ์ฐธ์กฐํ๊ณ ์์ผ๋ฏ๋ก ๋ฐ๋ก ์ ๊ทผ์ด ๋ถ๊ฐ๋ฅํ๋ค. ์ด๋ [class_name]_set์ ์ฌ์ฉํด์ ์ ๊ทผ์ด ๊ฐ๋ฅํ๋ค.
์ด ๋ _set ๋์ ์ ์ฌ์ฉํ ์ ์๋ ๊ฒ์ด related_name์ธ๋ฐ, ์ญ์ฐธ์กฐ ๋์์ธ ๊ฐ์ฒด๋ฅผ ๋ถ๋ฅผ ๋ ์ฌ์ฉํ๋ค.
์ฌ์ค related_name ์ค์ ๋ฐ์ดํฐ๋ฒ ์ด์ค ์์ ์ถ๊ฐ๋๋ ์์ฑ์ ์๋๊ณ ForeignKey ๋ก ์ค์ ๋์ด์ง๋ Accunt๋ชจ๋ธ์์ Posting ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํด์ผ ํ ๋ ์ฌ์ฉํ๋ ์์ฑ์ผ๋ก ์ฌ์ฉ๋๋ค.
๊ทธ๋ฌ๋๊น _set์ ์ฐ๋ related_name์ ์ฐ๋ ์ฌ์ค ๋น์ทํ๋ค. ํ์ง๋ง ์ธ์ ๋ ๊ทธ๋ ๋ฏ ์์ธ๊ฐ ์๋ ๋ฒ. ๐ณ
class Follow(models.Model):
following = models.ForeignKey(Account, max_length=7000, on_delete=models.CASCADE, related_name="following")
follower = models.ForeignKey(Account, max_length=7000, on_delete=models.CASCADE, related_name="follower")
class Meta:
db_table = "follows"
๋ค์๊ณผ ๊ฐ์ด ๋ชจ๋ธ ๋ ๊ฐ ์ด์์ FKํค๊ฐ ๊ฒน์น ์ ์๋๋ฐ, ์๋์ผ๋ก ์์ฑ๋๋ ์ด๋ฆ์ด ๊ฒน์น ์ ์์ผ๋ฏ๋ก related_name์ ๋ช ์ํด์ค์ ์ปดํจํฐ๊ฐ ํท๊ฐ๋ฆฌ์ง ์๊ฒ ๋ง๋๋ ๊ฒ์ด๋ค.
์ด๋๋ ํ์์ ์ผ๋ก related_name์ ์ฌ์ฉํด์ค์ผํ๋ค.
Account์์ Follow ๊ฐ์ฒด๋ฅผ ์ญ์ฐธ์กฐํ ๋, Account ๊ฐ์ฒด๋ฅผ ์ ์ฐธ์กฐํ๋ ๊ฐ์ฒด๊ฐ following๊ณผ follower 2๊ฐ์ด๋ค. _set๋ง์ผ๋ก๋ ์์ ์ ๋ฐ๋ผ๋ณด๋ ๋ ์์ฑ ์ค ์ด๋ค ์์ฑ์ ์ ๊ทผํด์ผํ ์ง ๋ชจ๋ฅด๊ธฐ ๋๋ฌธ์ด๋ค.
์ฒ์์๋ ๊ทธ๋ฅ ํ๋๋ช ์ ๋๊ฐ์ด ์ ์ด์ฃผ๋ฉด ๋๋๊ฑฐ ์๋์ผ? ๋ผ๊ณ ์๊ฐํ์๋ค. ๊ทผ๋ฐ ํ๋๋ช ๊ณผ ๋๊ฐ์ด ์ ์ด์ฃผ๋ฉด ๋ฌธ์ ๊ฐ ์๊ธด๋ค.
follower = models.ForeignKey(Account, max_length=7000, on_delete=models.CASCADE, related_name="follower")
๋ค์๊ณผ ๊ฐ์ด ์์ฑ์ ํด๋น follower์ Follow๋ฅผ ๋ถ๋ฌ์ฌ ๋, follower.follower.all() ์ฒ๋ผ ๊ฐ์ ธ์ ๋ฒ๋ฆฐ๋ค.
๋ฐ๋ผ์ related_name์ follwering์ผ๋ก ์จ์ค์ผํ๋ค. ๊ทธ๋ผ follower.following.all ์ด๋ฏ๋ก ์กฐ๊ธ ๋ ์๊ธฐ ์ฌ์์ง๋ ORM์ด ๋๊ธฐ ๋๋ฌธ์ด๋ค.
foreign key๋ก ์ฐ๊ฒฐ์์ผ ๋์ class์ related_name = following์ด๋ผ๊ณ ์ ์ผ๋ฉด follow.following์ด ์๊ธฐ๋ ๊ฒ์ด ์๋๋ผ ์ฐธ์กฐํ๋ Account(๋์ user ๋ชจ๋ธ๋ช ) ๋ชจ๋ธ์ ์๊ฒจ๋ฒ๋ฆฐ๋ค.
์ฐธ์กฐํด์ค ๊ฐ์ฒด ์ ์ฅ์์ ์๊ฐํด์ related_name์ ์์ฑํ์!
class Follow(models.Model):
following = models.ForeignKey(Account, max_length=7000, on_delete=models.CASCADE, related_name="follower")
follower = models.ForeignKey(Account, max_length=7000, on_delete=models.CASCADE, related_name="following")
class Meta:
db_table = "follows"
ManyToMany์ Foreign key์ ์ฐจ์ด๋ ์ค๊ฐํ ์ด๋ธ์ ๋ง๋๋ ์๋ง๋๋์ ์ฐจ์ด์ด๋ค.
1๋ ๋ค์ ํด๋นํ๋ Foreign key๋ ๊ด๊ณ ์ค์ ์ด 1๊ฐ๋ง ๋๊ธฐ ๋๋ฌธ์ ์ฐ๊ฒฐ๋ ๋ชจ๋ธ ์ค ์ด๋ ๊ณณ์ ์ถ๊ฐ๋๋๋์ ๋ฐ๋ผ ์๋ฏธ๊ฐ ๋ฌ๋ผ์ง๋ค.
์๋ ์์ฑ์ด ์๋๋ผ ๋ด๊ฐ ์ค๊ฐ ํ ์ด๋ธ์ ๋ง๋ค์ด์ ๊ด๋ฆฌํด์ผํ๋ค.
์ฐ๊ฒฐ๋์ด ์๋ ํ ํ ์ด๋ธ๊ณผ์ ์์ ์ ํ๋ ค๋ฉด ์ค๊ฐํ ์ด๋ธ์ ๊ฑฐ์ณ ๊ฐ์ ๋ฃ์ด์ค์ผํ๋ค. ๊ฐ์ ๋ถ๋ฌ์ฌ ๋๋ ์ค๊ฐ ํ ์ด๋ธ์ ๊ฐ๋ง ๋ถ๋ฌ์ฌ ์ ์๋ค.(๋ฌด์กฐ๊ฑด ์ง๊ฒ๋ค๋ฆฌ๋ก ์ด์ฉํด์ผํ๋ค๋ ๋ป)
ํ์ง๋ง ์ค๋ฌด์์๋ ํ๋ก์ ํธ๋ฅผ ํ๋ค๊ฐ ์ถ๊ฐ๋๋ ์์ฑ์ด๋ ๊ฐ์ด ์๊ธฐ ๋ง๋ จ์ด๋ฏ๋ก ์ง์ ๊ด๋ฆฌ๊ฐ ํ์ํ๋ค. ๋ฐ๋ผ์ ์ค๊ฐํ ์ด๋ธ์ ๋ง๋๋ ๊ฒ์ด ์ข๋ค.
ManyToMany ์์ฑ์ ์ฐ๊ฒฐํ ์ ์๋ ๊ด๊ณ์ ์์ ์ ํ์ด ์๋ค.
๋ ๋ชจ๋ธ์ ๊ด๊ณ๋ ์๋์์ฑ๋๋ Intermediate Table ์ ์ํด ๊ด๋ฆฌ๋๊ธฐ ๋๋ฌธ์ ํด๋น ๋ชจ๋ธ์ ํ ์ด๋ธ์๋ ํ๋๊ฐ ์์ฑ๋์ง ์๋๋ค.
์ค๊ฐ ํ ์ด๋ธ์ด ์์ผ๋ฏ๋ก ๋ฐ๋ก ์ฐ๊ฒฐ๊ฐ์ ์ถ๊ฐํ ์ ์์.
# ํ๋ก์ฐ model.py
class Follow(models.Model):
following = models.ForeignKey(Account, max_length=7000, on_delete=models.CASCADE, related_name="following")
follower = models.ForeignKey(Account, max_length=7000, on_delete=models.CASCADE, related_name="follower")
class Meta:
db_table = "follows"
# ํ๋ก์ฐ views.py
class FollowView(View):
def post(self, request):
try:
data = json.loads(request.body)
if Follow.objects.filter(following=data['following'], follower=data['follower']).exists():
Follow.objects.filter(following=data['following'], follower=data['follower']).delete()
Follow.objects.create(
following_id = data['following'],
follower_id = data['follower']
)
return JsonResponse({"MESSAGE":"SUCCESS"}, status=201)
except KeyError:
return JsonResponse({"MESSAGE":"KEY_ERROR"}, status=400)
def get(self, request):
try:
follows = Follow.objects.all()
result=[]
for follow in follows:
result.append({
'following' : follow.following_id,
'follower' : follow.follower_id
})
return JsonResponse({"RESULT":result, "MESSAGE":"SUCCESS"}, status=200)
except KeyError:
return JsonResponse({"MESSAGE":"ERROR"}, status=400)
models.py์์ ํ๋์ class์ ๋ํด ๋์ผํ class ๊ฐ์ฒด๊ฐ ๋๋ฒ FK๋ฅผ ์ง์ ํ ์ ์๊ธฐ ๋๋ฌธ์ related_name์ ์ด์ฉํ๋ค.
views.py์์๋ follow ๊ฐ์ฒด์ filter๋ฅผ ๊ฑธ๋ฌ ํ๋ก์๊ณผ ํ๋ก์ฐ ํ ๋ฐ์ดํฐ๋ฅผ ์ฐพ๊ณ ๋๋ฒ ํ๋ก์ฐ ํ ์ ์๊ฒ ํด๋น Account๊ฐ followingํน์ followerํ๋ instance๋ฅผ deleteํ๋ ๊ฒ์ด๋ค.
unfollow ๊ธฐ๋ฅ์ ์ถ๊ฐํ์ผ๋ฉด ์ข์์ ๊ฒ ๊ฐ๋ค.
์ ์ด์ ํ๋ก์ํ์ง ์์ ๊ด๊ณ์ ๋ํด์ ์ด๋ป๊ฒ ํ ๊ฒ์ธ์ง ์๊ฐํ์ง ์์๋ค.
์ฐธ๊ณ ์๋ฃ
related_name๊ณผ _set
https://swarf00.github.io/2019/09/06/associate-user.html
https://velog.io/@brighten_the_way/Django%EC%99%80-Reverse-relations%EA%B3%BC-Relatedname
ManyToMany vs Foreingkey
https://brunch.co.kr/@ddangdol/1
https://devbruce.github.io/django/dj-14-model_many+to+many/
์ค๊ฐํ
์ด๋ธ์ ๋ง๋๋ ์ด์
https://fierycoding.tistory.com/66?category=983255
related_name ์ด๋ฆ์ ๋ฌด์์ผ๋ก ์ธ ๊ฒ์ธ์ง์ ๋ํ ํฌ์คํ
https://fabl1106.github.io/django/2019/05/27/Django-26.-%EC%9E%A5%EA%B3%A0-related_name-%EC%84%A4%EC%A0%95%EB%B0%A9%EB%B2%95.html