3

Very similar to this question, but I tried the accepted answer and it did not work. Here's what's going on.

I have a form for tagging people in photos that looks like this:

forms.py

class TaggingForm(forms.Form):
    def __init__(self, *args, **kwargs):
        queryset = kwargs.pop('queryset')
        super(TaggingForm, self).__init__(*args, **kwargs)
        self.fields['people'] = forms.ModelMultipleChoiceField(required=False, queryset=queryset, widget=forms.CheckboxSelectMultiple)
...

models.py

class Photo(models.Model):
    user = models.ForeignKey(User)
...

class Person(models.Model):
    user = models.ForeignKey(User)
    photos = models.ManyToManyField(Photo)
...

I want users to be able to edit the tags on their photos after they initially tag them, so I have a page where they can go to view a single photo and edit its tags. For obvious reasons I want to have the already-tagged individuals' checkboxes pre-selected. I tried to do this by giving the form's initial dictionary a list of people I wanted selected, as in the answer to the question I linked above.

views.py

def photo_detail(request,photo_id):

    photo = Photo.objects.get(id=photo_id)

    initial = {'photo_id':photo.id, 'people':[p for p in photo.person_set.all()]}
    form_queryset = Person.objects.filter(user=request.user)

    if request.method == "POST":
        form = TaggingForm(request.POST, queryset=form_queryset)
        # do stuff
    else:
        form = TaggingForm(initial=initial, queryset=form_queryset)
...

When I try to initialize people as in the above code, the form doesn't show up, but no errors are thrown either. If I take the 'people' key/value pair out of the initial dictionary the form shows up fine, but without any people checked. Also I'm using Django 1.5 if that matters. Thanks in advance.

Community
  • 1
  • 1
snorthway
  • 576
  • 1
  • 10
  • 15
  • I think this would be helpful http://stackoverflow.com/questions/604266/django-set-default-form-values – Games Brainiac Jul 23 '13 at 16:45
  • Am I missing something? That answer seems to just be introducing the concept of initial values, which I already set. It's just the initial value for the checkboxes that doesn't work, and I don't know what to give it if not a list of the objects I want checked. – snorthway Jul 23 '13 at 17:00
  • Why not use django forms to do the work of settings initial values for you? Thats what I was trying to get at. – Games Brainiac Jul 23 '13 at 17:01
  • Sweet, setting the initial value in the form definition worked, although I have to pass the form an extra parameter. Thank you! – snorthway Jul 23 '13 at 17:11
  • Answered your question. Feel free to upvote and accept. – Games Brainiac Jul 23 '13 at 17:12

1 Answers1

2

What you could do is simply use django forms to handle all of this for you. Please refer to this question. Ideally it boils down to lettings djnago handle your forms and its validation and initial values.

Now this is actually a really good practice to get used to since, you're dissecting all your logic and your presentation. Its a great DRY principle.

Community
  • 1
  • 1
Games Brainiac
  • 80,178
  • 33
  • 141
  • 199