Home > database >  Save data from ChoiceField to database Django
Save data from ChoiceField to database Django

Time:02-18

I'm new to Django and I have built a Form that shows a single select field to chose from. The data in the field are calculated on the go by the Form.

I now need, once the data is being submitted, to be save on the database. The only problem is that, for some reason, I got an IntegrityError error NOT NULL constraint failed: manager_playlist.user_id

Below my view, form and model in Django

views.py

def playlist(request):
    if not is_user_already_auth_spotify(request):
        messages.error(request, "You're not authenticated with Spotify, please authenticate here")
        return redirect('/members/account/'   request.user.username)

    if request.method == "POST":
        form = ChoosePlaylistForm(request.POST, request=request)
        if form.is_valid():
            form.save()
            messages.success(request, "Playlist successfully chosen")
            return HttpResponseRedirect('account')
        else:
            pass
    else:
        form = ChoosePlaylistForm(request=request)

    return render(request, 'show_playlist.html', {"playlist_choose_form": form})

forms.py

class ChoosePlaylistForm(ModelForm):

    playlists = forms.ChoiceField(choices=())

    class Meta:
        model = Playlist
        fields = ('playlists',)

    def __init__(self, *args, request=None, **kwargs):
        super(ChoosePlaylistForm, self).__init__(*args, **kwargs)
        self.request = request
        self.fields['playlists'].choices = self.generate_selection()

    def generate_selection(self):
        sp_auth, cache_handler = spotify_oauth2(self.request)
        spotify = spotipy.Spotify(oauth_manager=sp_auth)
        s_user = spotify.current_user()
        u_playlists = spotify.user_playlists(s_user['id'], limit=10)

        choices = []
        for playlist in u_playlists["items"]:
            if playlist["owner"]["id"] == s_user['id']:
                playlist_choice = (playlist["id"], playlist["name"])
                choices.append(playlist_choice)
            else:
                pass

        return choices

model.py

class Playlist(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    playlists = models.CharField(max_length=50, null=True, blank=True)  # playlists are the ids of the playlists

    def __str__(self):
        return self.playlists

CodePudding user response:

The reason for the error is that when a new Playlist object is created, the user field must not be empty (you did not add null=True, and of course, would not make sense here if you did). Now the form validates because the form does not require the user field, only the playlists field. You have a couple of choices.

Option 1
Add the required field to your form (I haven't tested this, please check the docs!):

class ChoosePlaylistForm(ModelForm):

    playlists = forms.ChoiceField(choices=())

    class Meta:
        model = Playlist
        fields = ('playlists', 'user',)   # NOTE THE CHANGE HERE

    def __init__(self, *args, request=None, **kwargs):
        super(ChoosePlaylistForm, self).__init__(*args, **kwargs)
        self.request = request
        self.user = request.user    # Add the user to the form

Option 2
Save the form as is using commit=False, then add the missing field before you save your model:

if request.method == "POST":
        form = ChoosePlaylistForm(request.POST, request=request)
        if form.is_valid():
            playlist = form.save(commit=False)    # NOTE THE CHANGE HERE
            playlist.user = request.user          # Add the user to the partial playlist
            playlist.save()                       # Now you can save the playlist
            
            messages.success(request, "Playlist successfully chosen")
            return HttpResponseRedirect('account')

Option 3
Add the field when you instantiate the form itself (I'm not sure my syntax is correct here):

form = ChoosePlaylistForm(request.POST, request=request, instance=request.user)

EDIT
Option 3 above does not seem to work. I believe this edit will:

form = ChoosePlaylistForm(request.POST, request=request, initial={'user': request.user})
  • Related