E
E
evgenii-del2020-06-08 17:46:07
Django
evgenii-del, 2020-06-08 17:46:07

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

Serializer for this model
class TrackSerializer(serializers.ModelSerializer):
    musician = serializers.CharField(read_only=True)
    album = UserFilteredPrimaryKeyRelatedField(queryset=Album.objects)

    class Meta:
        model = Track
        fields = '__all__'

And a view to process
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)


5ede4eeb60563426775047.png

Text fields are substituted automatically, but how to make the picture and the track itself loaded too?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question