I'm completely confused why I'm getting this error:
__init__() got an unexpected keyword argument 'questions'

when I try to initiate CustomBaseFormSet.

I've been trying to hack together a Poll with dynamic Questions and Choices.
This has been much more difficult than I expected.

I started with the examples here:
http://www.pointy-stick.com/blog/2009/01/23/advanced-formset-usage-django/
but now I'm at a loss for how to resolve this.

thanks for any help,
Bryan


Django 1.1 beta 1
# # models.py
class Product(models.Model):
   # manufacturer = models.ForeignKey(Manufacturer)
   name = models.CharField(max_length=100)
   pub_date = models.DateTimeField('date published')

   def __unicode__(self):
       return u'%s' % (self.name)

class Poll(models.Model):
    product = models.ForeignKey(Product)
    name = models.CharField(max_length=100)

    def __unicode__(self):
        return self.name

class Question(models.Model):
    poll = models.ForeignKey(Poll)
    name = models.CharField(max_length=100)

    def __unicode__(self):
        return self.name

class Choice(models.Model):
    question = models.ForeignKey(Question)
    order = models.IntegerField(null=True, blank=True)
    name = models.CharField(max_length=200)

    def __unicode__(self):
        return self.name

class Answer(models.Model):
    poll = models.ForeignKey(Poll)
    question = models.ForeignKey(Question)
    user = models.ForeignKey(User)
    text = models.CharField(max_length=100)
    pub_date = models.DateTimeField('date published')

class AnswerForm(forms.Form):
    poll_id = forms.IntegerField()
    question = forms.IntegerField()
    # answer = forms.RadioSelect()
    text = forms.ChoiceField(widget=forms.RadioSelect())

    def __init__(self, *args, **kwargs):
        # for some reason this is giving an unscripable error
        question = kwargs.pop('question')

        super(AnswerForm, self).__init__(*args, **kwargs)

        choices = question.choice_set.order_by('order')
        self.fields['text'].choices = [(i, choice.name) for i, choice in
enumerate(choices)]
      # self.fields['text'].choices = [(0, u'Poor'), (1, u'Acceptable'), (2,
u'Excellent')]

from django.forms import formsets
# this is being called with:
# PollFormSet(data, questions=questions)
# error = __init__() got an unexpected keyword argument 'questions'
class CustomBaseFormSet(formsets.BaseFormSet):
    def __init__(self, *args, **kwargs):

        self.questions = kwargs["questions"]
        self.extra = len(self.questions)

        super(CustomBaseFormSet, self).__init__(*args, **kwargs)

    # this is to be deleted I'm just trying to initiate the debugger where I
think the error is happening
    # The set_trace() is not being called
    # this is called in the __init__ statement of BaseFromSet
    def _construct_forms(self):
        # instantiate all the forms and put them in self.forms
        self.forms = []
        # I tried to setup
        import pdb; pdb.set_trace()
        for i in xrange(self.total_form_count()):
            self.forms.append(self._construct_form(i))

    def _construct_form(self, i, **kwargs):
        kwargs['question'] = self.questions[i]
        return super(CustomBaseFormSet, self)._construct_form(i, **kwargs)

PollFormSet = formsets.formset_factory(AnswerForm, CustomBaseFormSet)

##################################################################
# views.py

def poll_detail(request, poll_id):
    """
    displays poll detail for the person to answer the questions in the Poll.
    if the request is a POST then process the form
    """

    if request.method == 'POST':
         formset = create_poll_formset(quiz_id, request.POST)
         if formset.is_valid():
             answers = [str(int(f.is_correct())) for f in formset.forms]
             return HttpResponseRedirect('%s?a=%s'
                     % (reverse('result-display', args=[poll_id]),
                         ''.join(answers)))
    else:
         formset = create_poll_formset(poll_id)

    return render_to_response('products/poll_detail.html', {'formset':
formset})

def create_poll_formset(poll_id, data=None):
    questions = Question.objects.filter(poll=poll_id)
    if not questions:
        # No poll found, must be a bad poll_id
        raise Http404("Invalid poll_id")

    return PollFormSet(data, questions=questions)

-- 
The best marketing related articles are at
http://www.InstantDirectMarketing.com

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to