Reputation: 2445
I reaaaally don´t know what else to do but come here and ask you guys. Heres the situation:
models.py
class Vote(models.Model):
""" Generic vote model """
user = models.ForeignKey(User)
question = models.ForeignKey(Question)
created = models.DateTimeField(auto_now_add=True)
objects = models.Manager()
cache = CacheVoteManager()
class Meta:
abstract = True
def __unicode__(self):
return '%s : %s' % (self.user.username, self.question.question)
class OptionVote(Vote):
option = models.ForeignKey(Option)
forms.py
class OptionChoiceField(forms.ModelChoiceField):
""" Custom model choice field for options """
widget = forms.RadioSelect(attrs={'class': 'c-opt'})
def label_from_instance(self, obj):
return mark_safe(
'<span class="c-opt-img">%s</span><span class="c-opt-name">%s</span>'
% (obj.media_content.draw_create_widget() , obj.name))
class OptionVoteForm(ModelForm):
""" Form to vote in a option-based question """
option = OptionChoiceField(queryset=OptionVote.objects.none(),
empty_label=None)
class Meta:
model = OptionVote
exclude = ['user', 'question']
def __init__(self, options=None, *args, **kwargs):
super(OptionVoteForm, self).__init__(*args, **kwargs)
if options:
self.fields['option'].queryset = options
views.py
form = OptionVoteForm(request.POST)
form.is_valid()
>> FALSE!!!!!!!!!!!!!!!
I have tried to see the errors in the form and there seems to be no one. I have put some flags on the form's clean method, and they don´t get called. Sames goes for clean method in OptionChoiceField.
The following code in the view
print 'PRINTING ERRORS: ' + str(form.errors)
for field in form:
print str(field.label_tag()) + ': ' + str(field.errors)
returns:
PRINTING ERRORS:
<label for="id_option_0">Option</label>:
please help me out here, i´m reaaally stuck in this one.
EDIT when i try to do
form = OptionVoteForm(request.POST)
print form
i get the following error:
Exception Type: AttributeError
Exception Value: 'QueryDict' object has no attribute 'all'
ExceptionLocation: /opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/forms/models.py in __iter__, line 882
oh, and btw, I´m using django 1.3
Upvotes: 0
Views: 880
Reputation: 2445
You're overriding the default constuctor so it accepts an queryset, so you should do:
qs = OptionVote.objects.all()
form = OptionVoteForm(qs, request.POST)
Upvotes: 5