tags:

views:

1866

answers:

4

Hi,

I'm working on my first Django application. In short, what it needs to do is to display a list of film titles, and allow users to give a rating (out of 10) to each film. I've been able to use the {{ form }} and {{ formset }} syntax in a template to produce a form which lets you rate one film at a time, which corresponds to one row in a MySQL table, but how do I produce a form that iterates over all the movie titles in the database and produces a form that lets you rate lots of them at once?

At first, I thought this was what formsets were for, but I can't see any way to automatically iterate over the contents of a database table to produce items to go in the form, if you see what I mean.

Currently, my views.py has this code:

def survey(request):
        ScoreFormSet = formset_factory(ScoreForm)
        if request.method == 'POST':
                formset = ScoreFormSet(request.POST, request.FILES)
                if formset.is_valid():
                        return HttpResponseRedirect('/')
        else:
                formset = ScoreFormSet()
        return render_to_response('cf/survey.html', {
                'formset':formset,
        })

And my survey.html has this:

<form action="/survey/" method="POST">
<table>
{{ formset }}
</table>
  <input type = "submit" value = "Submit">
</form>

Oh, and the definition of ScoreForm and Score from models.py are:

class Score(models.Model):
        movie = models.ForeignKey(Movie)
        score = models.IntegerField()
        user = models.ForeignKey(User)

class ScoreForm(ModelForm):
        class Meta:
                model = Score

So, in case the above is not clear, what I'm aiming to produce is a form which has one row per movie, and each row shows a title, and has a box to allow the user to enter their score.

If anyone can point me at the right sort of approach to this, I'd be most grateful.

Thanks,

Ben

+3  A: 

"At first, I thought this was what formsets were for, but I can't see any way to automatically iterate over the contents of a database table to produce items to go in the form, if you see what I mean."

You need to get a queryset. And you need to provide that queryset to your form as initial data. See using initial data with a formset for the code.

initial = [ list of { dictionaries }, one per form ]

Interestingly, this is a direct feature of the model API through the values method of a queryset.

S.Lott
Thank you! I've added: formset = oreFormSet(initial=Movie.objects.values()[:5])into views.py.It doesn't quite behave as I need, though: It produces a line per movie, but each line has a list box containing all movie titles, instead of just the one for that line. Is the query wrong or template?
Ben
(Sorry for shortness of that follow-up question -- I was constrained by the 300 character limit!) My sample of python code got corrupted too -- I'll try again: formset = ScoreFormSet(initial=Movie.objects.values()[:5])
Ben
Me again. I figured it out. My problem was that I was using the wrong Form...
Ben
When out of space in the comments, either update your question or ask a new question. Update when it's clarification. New when you've moved into a new problem that's clearly different from the original problem. Feel free to delete comments, also.
S.Lott
A: 

Hi, I have a similar need,

objects = models.AClass.objects.filter(condition) DetallesFormset = formset_factory(forms.AForm, extra=1) formset = AFormset(initial = objects)

but when In the last line, appears a type error, saying that object is not iterable.

HOw should I pass the initial parameters?

Thanks

A: 

I have found my answer, using modelformset_factory instead formset_factory solves the problem, Thanks...

A: 

i have a similar question. I am creating rows dyanmically in html using js. i want to add the values of multiple rows all at once to the database. suggest an approach......using django.....

arnov