python - Django: Creating a form per user on one page -
sorry title isn't descriptive. context: have event full of participants. when event on want leave feedback of other participants.
models.py
class feedback(models.model): action = models.foreignkey(action) feedback_by = models.foreignkey(userprofile, related_name='feedback_by') feedback_for = models.foreignkey(userprofile, related_name='feedback_for') comment = models.charfield(max_length=200) no_show = models.booleanfield() created = models.datetimefield() modified = models.datetimefield()
forms.py
class feedbackformset(basemodelformset): def add_fields(self, form, index): super(feedbackformset, self).add_fields(form, index) form.fields['is_checked'] = forms.booleanfield(required=false) class feedbackform(forms.modelform): comment = forms.charfield(label=(u"comment"), widget=forms.textarea()) class meta: model = feedback fields = ['comment', 'no_show']
i want create feedback page, there 1 instance of feedbackform each participant. after searching seems want using formset, i'm not finding documentation helpful , can't seem find examples.
if formset way go, guys me out (view/formset basically) starter code? if not, can point me should doing? thanks.
edit: i've added view , template code below.
views.py
@login_required def new_feedback(request, action_id): action = get_object_or_404(action, id=action_id) profile = userprofile.objects.get(user_id=request.user.id) participants = all_info_many_profiles(action.participants.filter(~q(id=profile.id))) fbformset = modelformset_factory(feedback, form=feedbackform, formset=feedbackformset) if request.method == 'post': formset = fbformset(request.post, request.files, queryset=action.participants.filter(~q(id=profile.id))) if formset.is_valid(): formset.save() else: print formset.errors else: formset = fbformset(queryset=action.participants.filter(~q(id=profile.id))) return render(request, 'app/new_feedback.html', {'action': action, 'participants': participants, 'formset': formset}
new_feedback.html
{% block body_block %} <h1>leave feedback</h1> {% participant in participants %} <li>{{ participant.username }}</li> {% endfor %} <form method="post" action=""> {% csrf_token %} {{ formset.management_form }} {% form in formset %} {{ form.as_p }} <br /> {% endfor %} <input type="submit" name="submit" value="submit feedback" /> </form> {% endblock %}
what i'm trying achieve associate 1 form each user in list (or queryset) of participants. have shows 1 more form want (for example, when list each user i'll see 3 names 4 forms example) , don't know if or how users/forms related.
the idea feedback_for field value automatically, ie in view do:
if formset.is_valid(): form in formset: = form.save(commit=false) a.feedback = participant a.save()
on top of that, added field "is_checked" intended specify whether i'm leaving feedback user or not. example of full functionality:
user1 [x] is_checked ... rest of form
user2 [] is_checked ... rest of form
user3 [x] is_checked ... rest of form
then when hit submit create 2 new entries in feedback table, 1 user1 , 1 user3
Comments
Post a Comment