python ForeignKey relation with user shows this error NOT NULL constraint failed: music_song.performer_id

北战南征 提交于 2021-01-29 15:45:15

问题


Fellow programmers, I am getting a error NOT NULL constraint failed: music_song.performer_id

I want the performer field to be the user who is creating the song but its not happening I have tried a lot can you guys please help me out with it

models.py

class Song(models.Model):
    name = models.CharField(_("Name"), max_length=50)
    song = models.FileField(_("Song"), upload_to=None, max_length=100)
    song_image = models.ImageField(upload_to=None, height_field=None, width_field=None,max_length=100)
    likes = models.IntegerField(_("Likes"),default=0)
    views = models.IntegerField(_("Views"),default=0)
    performer = models.ForeignKey(settings.AUTH_USER_MODEL,verbose_name=_("Performed By"),related_name='artist_in_song',on_delete=models.CASCADE)

serializers.py

 class SongCreateSerializer(serializers.ModelSerializer):
    performer = serializers.SerializerMethodField()

    def get_performer(self,obj):
        return obj.performer.id

    class Meta:
        model = Song
        fields = ['id','name','song','song_image','likes','views','performer']
        read_only_fields = ('performer','likes','views')

views.py

 class SongCreateView(generics.CreateAPIView):

     queryset = Song.objects.all()
     serializer_class = SongCreateSerializer
     permission_classes = [IsAuthenticated]

来源:https://stackoverflow.com/questions/61737335/python-foreignkey-relation-with-user-shows-this-error-not-null-constraint-failed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!