Im working with static files and making a soundcloud clone using drf for backend with aws as hosting for the files. when i make a post request with the files the backend returns this error
django.db.utils.IntegrityError: null value in column "name" of relation "cloudsound_song" violates not-null constraint
so i console.logged the data to see if it was returning empty or not, it wasn't. it looks like the backend doesnt want to accept the form data, before this i would get a 500 error.
here are my backend files
app/views.py
class SongViewSet(viewsets.ModelViewSet):
queryset = Song.objects.all()
content_type ='multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW'
serializer_class = SongSerializer
parser_classes = [parsers.MultiPartParser, parsers.FormParser, parsers.JSONParser, parsers.FileUploadParser]
http_method_names = ['get', 'post', 'patch', 'delete']
def perform_create(self, serializer):
serializer.save(owner=self.request.user)
app/serializers.py
class SongSerializer(serializers.HyperlinkedModelSerializer):
content_type ='multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW'
name = serializers.CharField(required=False )
image = serializers.FileField(required=False)
audio_file = serializers.FileField(required=False)
class Meta :
model = Song
fields = ('id', 'image', 'name', 'audio_file', 'created_on')
def create(self, validated_data):
song = Song.objects.create(
name=validated_data.get('name'),
image=validated_data.get('image'),
audio_file=validated_data.get('audio_file'),
)
song.save()
return song
app/models.py
class Song(models.Model):
name = models.CharField(max_length=100, )
image = models.FileField(upload_to='./media/', default='./media/default-cover-art_tVe9r28.png' )
audio_file = models.FileField(upload_to='./media/')
created_on = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.name
please let me know if this isnt enough.
i put null=true to see if it was just my form that was wrong or the post request but it wasnt it would post correctly the fields are just null.
{
"id": 31,
"image": null,
"name": null,
"audio_file":null,
"created_on": "2023-01-07T03:20:55.104324Z"
},
null=True
? image
, audio_file
and name
either have to have a value or have to have null=True
in the model declaration. Or they must have a default value declared in the model declaration like you did for image