0

A user will be able to list his property and he/she will be able to edit the listing.

The Problem

On editing, all other fields will show data already saved by the user and only amenities field won't display 'values already selected'.

All the values of the amenities field are saved in a TextField and user can select multiple items via 'CheckboxSelectMultiple' widget.

Models

class Motel(models.Model):
    user= models.ForeignKey(User)
    name= models.CharField(max_length=100, verbose_name='Name')
    amenities= models.TextField()

I added 'checked' to the form field widget but it will only check all the items.

class MotelForm(forms.ModelForm):
    amenities = forms.MultipleChoiceField(choices=FACILITY_CHOICES, required= False, widget=forms.CheckboxSelectMultiple(attrs={'checked':'checked'}))

Views

@login_required
def edit_motel_details(request, motel_id, slug):
    if id:
        motel= Motel.objects.get(id=motel_id, slug=slug)
    else:
        motel= motel()
    motel_form= MotelForm(instance=motel)

    MotelImagesInlineFormSet= inlineformset_factory(Motel, MotelImages, fields=('image',))
    formset= MotelImagesInlineFormSet(instance=motel)

    if request.method== "POST":
        motel_form= MotelForm(request.POST, request.FILES)

    if id:
        motel_form= MotelForm(request.POST, request.FILES, instance=motel)

        formset= MotelImagesInlineFormSet(request.POST, request.FILES, instance=motel)

        if motel_form.is_valid():
            created_motel= motel_form.save(commit=False)
            formset=MotelImagesInlineFormSet(request.POST, request.FILES, instance=created_motel)

            if formset.is_valid():
                created_motel.save()
                formset.save()
                redirect_url=reverse('listed')
                return HttpResponseRedirect(redirect_url) 
 return render(request, 'm/m_edit.html', {'motel_form': motel_form,'formset':formset,})

I want to display already selected/saved items by the user in the 'edit form'. What am I missing?

smack
  • 922
  • 11
  • 21
  • I don't understand your approach. `TextField` is a blob of text, `MultipleChoiceField` is a list of check boxes. How can you assume that a list of check boxes would be saved into a blob of text? – Shang Wang Jan 06 '17 at 19:48
  • It saved. Even if I put it in a CharField it will still save. I don't want to keep writing a model filled with 100's of items when I can do that with a line and save and retrieve. It saved like this [u'Wireless Internet', u'Air Condition', u'Swimming Pool', u'Television']. Django didn't explicitly state the 'only' data that can be saved in the field. – smack Jan 06 '17 at 20:36
  • That's part of my point. You saved choices as a string, but there's no django documentation said that django is smart enough to recover a string back to a list of check boxes. – Shang Wang Jan 06 '17 at 20:41
  • And that's why I'm here to seek for advice. Someone might have faced this before. – smack Jan 06 '17 at 20:44
  • I did a simple research and found this: http://stackoverflow.com/questions/5335603/in-django-how-do-you-save-multiple-form-fields-into-a-single-model-field-as-a-l – Shang Wang Jan 06 '17 at 20:46
  • Thanks. But it doesn't give a clue of getting through with this. – smack Jan 06 '17 at 20:51

1 Answers1

0

In the Meta of your MotelForm add amenities in fields.

class MotelForm(forms.ModelForm):
    amenities = forms.MultipleChoiceField(choices=FACILITY_CHOICES, required=False, widget=forms.CheckboxSelectMultiple())

    class Meta:
        model = Motel
        fields = ['name', 'amenities', ...]

Now when you will pass the instance it should load the values already selected.

Pranav Aggarwal
  • 605
  • 4
  • 9