3

I have a form with Django autocomplete light working and saving fine. However, I also have a link to "Edit". I then pre-populate the form with the intial data that they've saved except that Django autocomplete light field's data is not being shown when in edit mode

Here is my edit form method:

@login_required(login_url='/')
def edit_credits(request, template_name):
    user = request.user
    credits_profile = get_object_or_404(CreditOptions, user=user)
    credits = CreditOptions.objects.filter(user=user)
    credits_data = list(credits.values())
    if credits_profile.user != user:
        return HttpResponseForbidden("Sorry, something here doesn't make sense. Please try again.")

    if request.method == 'POST':
        formset = OptionFormset(request.POST, instance=credits_profile)
        if formset.is_valid():
            for form in formset.forms:
                instance = form.save(commit=False)
                instance.user = user
                instance.save()
            messages.success(request, "Successfully updated your credits.")
            return redirect(urlresolvers.reverse('skills'))
    else:
        formset = OptionFormset(initial=credits_data)
    return render(request, template_name, {
        'formset': formset,
    })

Here is my forms.py

class CreditOptionsForm(forms.ModelForm):
    crew_position = forms.ModelChoiceField(CrewPosition.objects.all(), 
        widget=autocomplete_light.ChoiceWidget('CrewPositionAutocomplete'), 
        required=False, 
        help_text='Start typing and it will auto-suggest for you. e.g: PRODUCER')

    class Meta:
        model = CreditOptions
        exclude = ('user',)

OptionFormset = formset_factory(CreditOptionsForm, extra=1)

What I would like to have for the initial data is something like this: initial data desired

that way they can "x" out the data and replace it.

Any suggestions?

Thanks!

jeffci
  • 2,537
  • 6
  • 37
  • 59
  • So you're saying that initial_data doesn't work at all right ? It's weird that you're passing a list to initial_data – jpic Oct 31 '14 at 18:31
  • @jpic, it's a list because that's what the formset_factory is expecting to be there, it builds a form with multiple items – jeffci Oct 31 '14 at 19:03
  • Right, I thought we had to pass a list of dict to initial instead: https://docs.djangoproject.com/en/1.7/topics/forms/formsets/#using-initial-data-with-a-formset but apparently, it seems like you're passing a list of values (correct me if i'm wrong, i didn't go through the process of reproducing your issue locally (yet)) – jpic Nov 01 '14 at 02:03

0 Answers0