Skip to content
This repository has been archived by the owner on Jan 28, 2023. It is now read-only.

Commit

Permalink
ManyToManyField 예시 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
askcompany-kr committed Feb 2, 2020
1 parent 78a9337 commit 50d6578
Show file tree
Hide file tree
Showing 4 changed files with 390 additions and 1 deletion.
344 changes: 344 additions & 0 deletions ManyToManyField Demo.ipynb
@@ -0,0 +1,344 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"os.environ['DJANGO_SETTINGS_MODULE'] = 'askcompany.settings'\n",
"os.environ[\"DJANGO_ALLOW_ASYNC_UNSAFE\"] = \"true\"\n",
"\n",
"import django\n",
"django.setup()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"from instagram.models import Post, Tag"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<Post: 네 번째 포스팅>"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"post = Post.objects.first()\n",
"post"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<QuerySet [<Tag: 파이썬>]>"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"post.tag_set.all()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<Tag: 장고>"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Tag.objects.create(name='장고')"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<Tag: AskCompany>"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Tag.objects.create(name='AskCompany')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<QuerySet [<Tag: 파이썬>, <Tag: 장고>, <Tag: AskCompany>]>"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Tag.objects.all()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<Tag: 파이썬>"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tag = Tag.objects.first()\n",
"tag"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<QuerySet [<Post: 네 번째 포스팅>]>"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tag.post_set.all()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<QuerySet [<Tag: 파이썬>]>"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"post.tag_set.all()"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [],
"source": [
"# tag = Tag.objects.get(name='장고')\n",
"# post.tag_set.remove(tag)\n",
"\n",
"tag_qs = Tag.objects.all()\n",
"post.tag_set.add(*tag_qs) # unpack"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<QuerySet [<Tag: 파이썬>, <Tag: 장고>, <Tag: AskCompany>]>"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"post.tag_set.all()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [],
"source": [
"def myfn(a, b, c):\n",
" print(f\"a = {a}, b = {b}, c = {c}\")"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"a = 1, b = 2, c = 3\n"
]
}
],
"source": [
"myfn(1, 2, 3)"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"a = 1, b = 2, c = 3\n"
]
}
],
"source": [
"params = [1, 2, 3]\n",
"myfn(*params)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
7 changes: 6 additions & 1 deletion instagram/admin.py
@@ -1,7 +1,7 @@
from django.contrib import admin
from django.utils.safestring import mark_safe

from .models import Post, Comment
from .models import Post, Comment, Tag


@admin.register(Post) # Wrapping
Expand All @@ -23,3 +23,8 @@ def message_length(self, post):
@admin.register(Comment)
class CommentAdmin(admin.ModelAdmin):
pass


@admin.register(Tag)
class TagAdmin(admin.ModelAdmin):
pass
31 changes: 31 additions & 0 deletions instagram/migrations/0006_auto_20200202_0854.py
@@ -0,0 +1,31 @@
# Generated by Django 3.0.2 on 2020-02-02 08:54

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('instagram', '0005_post_author'),
]

operations = [
migrations.CreateModel(
name='Tag',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=50, unique=True)),
],
),
migrations.AlterField(
model_name='comment',
name='post',
field=models.ForeignKey(limit_choices_to={'is_public': True}, on_delete=django.db.models.deletion.CASCADE, to='instagram.Post'),
),
migrations.AddField(
model_name='post',
name='tag_set',
field=models.ManyToManyField(blank=True, to='instagram.Tag'),
),
]

0 comments on commit 50d6578

Please sign in to comment.