diff --git a/ManyToManyField Demo.ipynb b/ManyToManyField Demo.ipynb new file mode 100644 index 0000000..00db21f --- /dev/null +++ b/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": [ + "" + ] + }, + "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": [ + "]>" + ] + }, + "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": [ + "" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "Tag.objects.create(name='장고')" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "" + ] + }, + "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": [ + ", , ]>" + ] + }, + "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": [ + "" + ] + }, + "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": [ + "]>" + ] + }, + "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": [ + "]>" + ] + }, + "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": [ + ", , ]>" + ] + }, + "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 +} diff --git a/instagram/admin.py b/instagram/admin.py index 8f392f1..1dcc4eb 100644 --- a/instagram/admin.py +++ b/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 @@ -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 diff --git a/instagram/migrations/0006_auto_20200202_0854.py b/instagram/migrations/0006_auto_20200202_0854.py new file mode 100644 index 0000000..3ce7530 --- /dev/null +++ b/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'), + ), + ] diff --git a/instagram/models.py b/instagram/models.py index 93d166f..6b8e2cd 100644 --- a/instagram/models.py +++ b/instagram/models.py @@ -6,6 +6,7 @@ class Post(models.Model): author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE) message = models.TextField() photo = models.ImageField(blank=True, upload_to='instagram/post/%Y/%m/%d') + tag_set = models.ManyToManyField('Tag', blank=True) is_public = models.BooleanField(default=False, verbose_name='공개여부') created_at = models.DateTimeField(auto_now_add=True) updated_at = models.DateTimeField(auto_now=True) @@ -30,3 +31,11 @@ class Comment(models.Model): message = models.TextField() created_at = models.DateTimeField(auto_now_add=True) updated_at = models.DateTimeField(auto_now=True) + + +class Tag(models.Model): + name = models.CharField(max_length=50, unique=True) + # post_set = models.ManyToManyField(Post) + + def __str__(self): + return self.name