From 65ad4a5f21599efb1f199878b7ae4927e57e9e7c Mon Sep 17 00:00:00 2001 From: keannu125 Date: Wed, 1 Mar 2023 22:58:22 +0800 Subject: [PATCH] Notes now has owner foreign key --- project/db.sqlite3 | Bin 151552 -> 151552 bytes project/notes/migrations/0006_note_owner.py | 21 ++++++++++++++++++ .../notes/migrations/0007_alter_note_owner.py | 21 ++++++++++++++++++ .../notes/migrations/0008_alter_note_owner.py | 21 ++++++++++++++++++ project/notes/models.py | 3 +++ project/notes/views.py | 10 +++++++-- 6 files changed, 74 insertions(+), 2 deletions(-) create mode 100644 project/notes/migrations/0006_note_owner.py create mode 100644 project/notes/migrations/0007_alter_note_owner.py create mode 100644 project/notes/migrations/0008_alter_note_owner.py diff --git a/project/db.sqlite3 b/project/db.sqlite3 index 4b4b685f771c003d0c153eee6c041e7cfc2ef6cd..a15a2912cf476160b42332e0e70a1ecc4725f8c8 100644 GIT binary patch delta 2360 zcmai0TWs4@81|o=i?q>ey^cs{CzG}}+HTrD zvW?lgsXwVJ)JZBuHCV4#Q_gp+J7lk!U=pcPxl)+oI4+sVj#mnk8MU7&PnQalJjcg- zIsA{YF`-{b_KSR9OiT!z5ci-x*r5fXadBkBQ(6GaJq;1TqI zAc>NURy?vEhzqhLA;BBf1H2?BVrbgSZ35zQf#tX3+>xo#=w|jl0AGf2uR&;M~ko2v#+XjOqKKmfjWksn$&!7jl0})cwrPWdw zc0esJh)*;K_mZp1>Wo!sH4Z+yR1Ug4==HjB+uud1+UjIgoW`t<4EEu_ zJ_jD<{W5q+b_cg1(x9xA!AaU|2JmX6%li*?90Qo>_)hSiF+%`&ZYGwyLO3YQ4Y>Mq^cMz6r3#WguI>OX`bTwv&#GDw6Tt=I1#DnGfL;V zfkQ9WNkJzKk(VIw{3pAV@$U0?sc1nUou`##_a|l>V>P`vUSwE!svWb8xU#uYtOy|_ zH|0N?3*wMCEt=LhIO#_yz|YwH61GL-9eKHXTwllg!?4 zI#`Gp5gl#q*qITRv{Tx=p93ROC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gkO3 z=rPL~HTfy3#r4N;fIaImK-|QLn`pPaM%agSUi^NGSk2r4@fJqBg?#=%z&@P|zxSN0 zo29)|!HDZbmKf>0*{L9y`<*p+yZ*8ZLF@cTKkVE%>J{G^#QzI>-DJMnQ z1sd`cgS*?b*klNW7$=KyD4|OpPs;-8nUtb&QHa)Po`zA8|j;Nr{!nI3=N&A~G+s=*=giBZY!IqVm@5UHh>Dz3`!6rx2+EY*Qz# zov>o)Zf7OFPwcf3UNjG#L~UGm`D4#QyK4M#nbvE@_an1*$fi!i@llK&A*^jocNzb< zq%vhw4%OcP diff --git a/project/notes/migrations/0006_note_owner.py b/project/notes/migrations/0006_note_owner.py new file mode 100644 index 0000000..4ffa86d --- /dev/null +++ b/project/notes/migrations/0006_note_owner.py @@ -0,0 +1,21 @@ +# Generated by Django 4.1.7 on 2023-03-01 13:47 + +from django.conf import settings +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ('notes', '0005_delete_historicalnote'), + ] + + operations = [ + migrations.AddField( + model_name='note', + name='owner', + field=models.ForeignKey(default=0, on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL), + ), + ] diff --git a/project/notes/migrations/0007_alter_note_owner.py b/project/notes/migrations/0007_alter_note_owner.py new file mode 100644 index 0000000..c307cfd --- /dev/null +++ b/project/notes/migrations/0007_alter_note_owner.py @@ -0,0 +1,21 @@ +# Generated by Django 4.1.7 on 2023-03-01 13:51 + +from django.conf import settings +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ('notes', '0006_note_owner'), + ] + + operations = [ + migrations.AlterField( + model_name='note', + name='owner', + field=models.ForeignKey(blank=True, on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL), + ), + ] diff --git a/project/notes/migrations/0008_alter_note_owner.py b/project/notes/migrations/0008_alter_note_owner.py new file mode 100644 index 0000000..cfc1777 --- /dev/null +++ b/project/notes/migrations/0008_alter_note_owner.py @@ -0,0 +1,21 @@ +# Generated by Django 4.1.7 on 2023-03-01 14:48 + +from django.conf import settings +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ('notes', '0007_alter_note_owner'), + ] + + operations = [ + migrations.AlterField( + model_name='note', + name='owner', + field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL), + ), + ] diff --git a/project/notes/models.py b/project/notes/models.py index 78a5a74..b2363f6 100644 --- a/project/notes/models.py +++ b/project/notes/models.py @@ -1,5 +1,7 @@ from django.db import models from django.utils.timezone import now +from django.contrib.auth import get_user_model +User = get_user_model() # Create your models here. @@ -7,6 +9,7 @@ class Note(models.Model): title = models.CharField(max_length=20) content = models.CharField(max_length=1024) date_created = models.DateTimeField(default=now, editable=False) + owner = models.ForeignKey(User, on_delete=models.CASCADE) def __str__(self): return self.title diff --git a/project/notes/views.py b/project/notes/views.py index a7165a7..957dcea 100644 --- a/project/notes/views.py +++ b/project/notes/views.py @@ -1,9 +1,15 @@ -from django.shortcuts import render +from rest_framework.permissions import IsAuthenticated from rest_framework import viewsets from .serializers import NoteSerializer from .models import Note class NoteViewSet(viewsets.ModelViewSet): - queryset = Note.objects.all().order_by('date_created') + permission_classes = [IsAuthenticated] serializer_class = NoteSerializer + queryset = Note.objects.all() + + def get_queryset(self): + user = self.request.user + queryset = Note.objects.filter(owner=user).order_by('date_created') + return queryset