问题
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