Notes now has owner foreign key

This commit is contained in:
keannu125 2023-03-01 22:58:22 +08:00
parent dbb55fff06
commit 65ad4a5f21
6 changed files with 74 additions and 2 deletions

Binary file not shown.

View file

@ -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),
),
]

View file

@ -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),
),
]

View file

@ -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),
),
]

View file

@ -1,5 +1,7 @@
from django.db import models from django.db import models
from django.utils.timezone import now from django.utils.timezone import now
from django.contrib.auth import get_user_model
User = get_user_model()
# Create your models here. # Create your models here.
@ -7,6 +9,7 @@ class Note(models.Model):
title = models.CharField(max_length=20) title = models.CharField(max_length=20)
content = models.CharField(max_length=1024) content = models.CharField(max_length=1024)
date_created = models.DateTimeField(default=now, editable=False) date_created = models.DateTimeField(default=now, editable=False)
owner = models.ForeignKey(User, on_delete=models.CASCADE)
def __str__(self): def __str__(self):
return self.title return self.title

View file

@ -1,9 +1,15 @@
from django.shortcuts import render from rest_framework.permissions import IsAuthenticated
from rest_framework import viewsets from rest_framework import viewsets
from .serializers import NoteSerializer from .serializers import NoteSerializer
from .models import Note from .models import Note
class NoteViewSet(viewsets.ModelViewSet): class NoteViewSet(viewsets.ModelViewSet):
queryset = Note.objects.all().order_by('date_created') permission_classes = [IsAuthenticated]
serializer_class = NoteSerializer 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