Commit 4eda615e authored by onny's avatar onny
Browse files

fix submitting project for artist, add custom field slug

parent 28aba9d1
......@@ -2,13 +2,34 @@ from .models import Artist, Project
from rest_framework import serializers
class ArtistSerializer(serializers.ModelSerializer):
slug = serializers.SerializerMethodField(read_only=True)
def get_slug(self, obj):
name = obj.name
name = name.replace(" ","-")
name = name.lower()
return name
class Meta:
model = Artist
fields = ('id', 'name')
fields = ('id', 'name', 'slug')
class ProjectSerializer(serializers.ModelSerializer):
class Meta:
model = Project
fields = ('id', 'artist', 'name')
class ProjectReadSerializer(serializers.ModelSerializer):
artist = ArtistSerializer(many=False, read_only=True)
slug = serializers.SerializerMethodField(read_only=True)
def get_slug(self, obj):
name = obj.name
name = name.replace(" ","-")
name = name.lower()
return name
class Meta:
model = Project
fields = ('id', 'artist', 'name')
fields = ('id', 'artist', 'name', 'slug')
......@@ -2,7 +2,7 @@ from django.template.response import TemplateResponse
from django.http import HttpResponse
from .models import Artist, Project
from rest_framework import viewsets
from .serializers import ArtistSerializer, ProjectSerializer
from .serializers import ArtistSerializer, ProjectSerializer, ProjectReadSerializer
def index_page(request):
html = TemplateResponse(request, 'index.html')
......@@ -44,8 +44,9 @@ class ArtistViewSet(viewsets.ModelViewSet):
serializer_class = ArtistSerializer
class ProjectViewSet(viewsets.ModelViewSet):
"""
API endpoint that allows artists to be viewed or edited.
"""
queryset = Project.objects.all()
serializer_class = ProjectSerializer
def get_serializer_class(self):
if self.request.method in ['GET']:
return ProjectReadSerializer
return ProjectSerializer
......@@ -23,7 +23,7 @@ var demo = new Vue({
addProject: function () {
var newProject = {
name: this.project.name.trim(),
"artist.id": this.artist.id
artist: this.artist.id
};
axios
.post('http://127.0.0.1:8000/api/project/', newProject);
......
......@@ -7,7 +7,7 @@
<div v-for="(artist, index) in artist" class="artist">
<div class="col-sm-12">
<img src="https://uploads.wikiart.org/Content/images/ARTIST-480x600.jpg" width=100px>
<br><b>[[ artist.name ]]</b><br>
<br><b><a :href="'/' + artist.slug">[[ artist.name ]]</a></b><br>
<button class="btn" v-on:click="removeArtist([[ index ]])">Delete</button>
</div>
</div>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment