Answer the question
In order to leave comments, you need to log in
How to automatically upload a file on update?
I have a track model
class Track(models.Model):
prepopulated_fields = {'slug': ('title',)}
musician = models.ForeignKey(User, on_delete=models.CASCADE)
title = models.CharField(max_length=150)
description = models.TextField(null=True, blank=True)
track = models.FileField(upload_to="music")
image = models.ImageField(upload_to="tracks/", null=True, blank=True)
slug = models.SlugField(max_length=150)
genre = models.ForeignKey(Genre, on_delete=models.SET_NULL, null=True, blank=True)
album = models.ForeignKey(Album, on_delete=models.SET_NULL, related_name="tracks", null=True, blank=True)
def __str__(self):
return self.title
class TrackSerializer(serializers.ModelSerializer):
musician = serializers.CharField(read_only=True)
album = UserFilteredPrimaryKeyRelatedField(queryset=Album.objects)
class Meta:
model = Track
fields = '__all__'
class TrackViewSet(viewsets.ModelViewSet):
queryset = Track.objects.all()
serializer_class = TrackSerializer
permission_classes = (IsOwnerOrReadOnly, permissions.IsAuthenticatedOrReadOnly)
def perform_create(self, serializer):
serializer.save(musician=self.request.user)
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question