diff --git a/project/accounts/serializers.py b/project/accounts/serializers.py new file mode 100644 index 0000000..060f147 --- /dev/null +++ b/project/accounts/serializers.py @@ -0,0 +1,12 @@ +from django.contrib.auth.models import User +from rest_framework import serializers +from notes.models import Note + + +class CustomUserSerializer(serializers.ModelSerializer): + notes = serializers.PrimaryKeyRelatedField( + many=True, queryset=Note.objects.all()) + + class Meta: + model = User + fields = ['id', 'username', 'notes'] diff --git a/project/config/settings.py b/project/config/settings.py index 3a92483..8bab837 100644 --- a/project/config/settings.py +++ b/project/config/settings.py @@ -41,7 +41,8 @@ INSTALLED_APPS = [ 'rest_framework.authtoken', 'notes.apps.NotesConfig', 'corsheaders', - 'djoser' + 'djoser', + 'accounts', ] MIDDLEWARE = [ @@ -146,6 +147,9 @@ DJOSER = { 'SEND_ACTIVATION_EMAIL': True, 'SEND_CONFIRMATION_EMAIL': True, 'ACTIVATION_URL': 'activation/{uid}/{token}', + 'SERIALIZERS': { + 'user': 'accounts.serializers.CustomUserSerializer' + }, } EMAIL_HOST = 'sandbox.smtp.mailtrap.io' diff --git a/project/db.sqlite3 b/project/db.sqlite3 index a15a291..68766b5 100644 Binary files a/project/db.sqlite3 and b/project/db.sqlite3 differ diff --git a/project/notes/serializers.py b/project/notes/serializers.py index c4c74cd..d3c12d3 100644 --- a/project/notes/serializers.py +++ b/project/notes/serializers.py @@ -3,6 +3,8 @@ from .models import Note class NoteSerializer(serializers.HyperlinkedModelSerializer): + owner = serializers.ReadOnlyField(source='owner.username') + class Meta: model = Note - fields = ('id', 'title', 'content', 'date_created') + fields = ('id', 'title', 'content', 'date_created', 'owner') diff --git a/project/notes/views.py b/project/notes/views.py index 957dcea..ee53847 100644 --- a/project/notes/views.py +++ b/project/notes/views.py @@ -13,3 +13,6 @@ class NoteViewSet(viewsets.ModelViewSet): user = self.request.user queryset = Note.objects.filter(owner=user).order_by('date_created') return queryset + + def perform_create(self, serializer): + serializer.save(owner=self.request.user)