On Jul 5, 2017 6:55 AM, "elloy" <eleni...@gmail.com> wrote:
>
> I need your help with a problem I'm trying to solve
> I'm making a questionnaire and I have to count the selected choices that the user have checked in a certain question with 5 possible answers(choices)
> The model I'm using is:
>
> class Reuse(models.Model):
> REUSE_OPTIONS = (
> ('1', 'first choice'),
> ('2', 'second choice'),
> ('3', 'third choice'),
> ('4', 'fourth choice'),
> ('5', 'none of the above'),
> )
>
> user = models.ForeignKey(settings.AUTH_USER_MODEL, null=True, blank=True)
> reuse = MultiSelectField(max_length=250, choices= REUSE_OPTIONS, max_choices=4, null=True)
> m = models.DecimalField(max_digits=5, decimal_places=3, default=0)
> numchoices = models.IntegerField()
I am not an expert in Django, but I will point out some problems. You just made an assignment to numchoices, then you redefine numchoices in the next line.
> def numchoices(self): (I tried this but it didn't work)
Telling us it didn't work is not helpful. You need to be much more explicit. What led you to say this didn't work?
> self.choices_count = self.choice_set.count()
What are choices_count any choice_set?
> self.save()
>
> I wrote this view:
> def reuse (request):
> if request.method == "POST":
> form = ReuseForm(request.POST)
> if form.is_valid():
> reuse = form.save(commit=False)
> # if reuse.get_choices.count() == 1 : reuse.m=0.075 (this doesn't work either)
> # elif reuse.get_choices.count() == 2 : reuse.m=0.15
> # elif reuse.get_choices.count() == 3 : reuse.m=0.225
> # elif reuse.get_choices.count() == 4 : reuse.m=0.3
Rather than calling the method so many times it's better to call it once and assign the result to a variable. Also in this case you can compute m from the count
> reuse.save()
> return redirect('../22')
> else:
> form = ReuseForm()
> return render(request, 'questionnaire/reuse.html', {'form':form})
>
> And the form I'm using is:
>
> class ReuseForm(forms.ModelForm):
>
> class Meta:
> model = Reuse
> fields = ('reuse',)
>
> Please, have you got any suggestions;
By the way I don't know what the effect is of defining a function in a model. Someone else will have to deal with that. I suggest you fix problems I've pointed out run the program tell us exactly what goes wrong and where
>
>
>
> --
> You received this message because you are subscribed to the Google Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
> To post to this group, send email to django...@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/ca1f528d-903a-428f-b781-6506dff69e0f%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
AUTH_USER_MODEL, null=True, blank=True)
reuse = MultiSelectField(max_length=250, choices= REUSE_OPTIONS, max_choices=4, null=True)
m = models.DecimalField(max_digits=5, decimal_places=3, default=0)
views.py:
def reuse (request):
if request.method == "POST":
form = ReuseForm(request.POST)
if form.is_valid():
reuse = form.save(commit=False)
q = reuse.get_choices.count()
if q == 1 : reuse.m=0.075
elif q == 2 : reuse.m=0.15
elif q == 3 : reuse.m=0.225
elif q == 4 : reuse.m=0.3
reuse.save()
return redirect('../22')
else:
form = ReuseForm()
return render(request, '../reuse.html', {'form':form})
Exception Type: AttributeError Exception Value: 'Reuse' object has no attribute 'get_choices'