mirror of
https://github.com/lemeow125/Django-NotesApp.git
synced 2024-11-17 06:29:25 +08:00
Notes now has owner foreign key
This commit is contained in:
parent
dbb55fff06
commit
65ad4a5f21
6 changed files with 74 additions and 2 deletions
Binary file not shown.
21
project/notes/migrations/0006_note_owner.py
Normal file
21
project/notes/migrations/0006_note_owner.py
Normal 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),
|
||||||
|
),
|
||||||
|
]
|
21
project/notes/migrations/0007_alter_note_owner.py
Normal file
21
project/notes/migrations/0007_alter_note_owner.py
Normal 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),
|
||||||
|
),
|
||||||
|
]
|
21
project/notes/migrations/0008_alter_note_owner.py
Normal file
21
project/notes/migrations/0008_alter_note_owner.py
Normal 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),
|
||||||
|
),
|
||||||
|
]
|
|
@ -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
|
||||||
|
|
|
@ -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
|
||||||
|
|
Loading…
Reference in a new issue