Saturday, June 30, 2018

Cannot add choice filed with radio buttons in my model form using widget but only charfield.

post-form.html
{% extends "posts/post_base.html" %}

{% load bootstrap3 %}

{% block post_content %}
<h4>{% if not form.instance.pk %}
Create Post
{% else %}
Update Post
{% endif %}</h4>
<form method="POST" id="postForm">
{% csrf_token %}
{% bootstrap_form form %}
<input type="submit" value="Post" class="btn btn-primary btn-large">
</form>
{% endblock %}


models.py
from django.conf import settings
from django.urls import reverse
from django.db import models

import misaka

from django.contrib.auth import get_user_model
User = get_user_model()

class Teacher(models.Model):
    teacher_name = models.CharField(blank=False, max_length=100, unique=True, null=False)

    def __str__(self):
        return self.teacher_name

class Subject(models.Model):
    subject_name = models.CharField(blank=False, max_length=100, unique=True, null=False)

    def __str__(self):
        return self.subject_name

class Post(models.Model):

    user = models.ForeignKey(User, related_name="posts", on_delete=models.CASCADE)
    teacher = models.ForeignKey(Teacher, related_name="posts", on_delete=models.CASCADE, null = False, blank = False)
    subject = models.ForeignKey(Subject, related_name="posts", on_delete=models.CASCADE, null = False, blank = False)
    created_at = models.DateTimeField(auto_now=True)
    param1 = models.PositiveIntegerField(blank=False, null=False, verbose_name = "The objectives of this course were made clear to me by this teacher.")
    param2 = models.PositiveIntegerField(blank=False, null=False, verbose_name = "The teacher speaks, articulates and explains concepts clearly.")
    param3 = models.PositiveIntegerField(blank=False, null=False, verbose_name = "The teacher adheres to the timings schedule and enforces discipline in the class.")
    param4 = models.PositiveIntegerField(blank=False, null=False, verbose_name = "Interest generated by the teacher.")
    param5 = models.PositiveIntegerField(blank=False, null=False, verbose_name = "The lectures were well structured and focused on the topics.")
    param6 = models.PositiveIntegerField(blank=False, null=False, verbose_name = "Accessibility of the teacher in and out of the class.")
    param7 = models.PositiveIntegerField(blank=False, null=False, verbose_name = "The teacher has fair knowledge on the subject matter.")
    param8 = models.PositiveIntegerField(blank=False, null=False, verbose_name = "Effective use of teaching aids.")
    param9 = models.PositiveIntegerField(blank=False, null=False, verbose_name = "Time spend on lecturing by teacher for course coverage was sufficient and lesson plan was followed.")
    param10 = models.PositiveIntegerField(blank=False, null=False, verbose_name = "The teacher encourage students to raise pertinent questions and answer them.")
    average = models.FloatField()
    message = models.CharField(blank=False, max_length=100, null=True, verbose_name = "Anything else you want to say.")

    def __str__(self):
        return self.teacher.teacher_name

    def get_absolute_url(self):
        return reverse(
            "posts:single",
            kwargs={
                "username": self.user.username,
                "pk": self.pk
            }
        )

    def save(self, *args, **kwargs):
        self.average = (self.param1 + self.param2 + self.param3 + self.param4 + self.param5 + self.param6 + self.param7 +self. param8 + self.param9 + self.param10)/10
        super().save(*args, **kwargs)

    class Meta:
        ordering = ["-created_at"]
        unique_together = ["user", "teacher", "subject"]


forms.py
from django import forms

from posts import models


class PostForm(forms.ModelForm):

    class Meta:
        model = models.Post
        fields = ("teacher","subject","param1","param2","param3","param4",
                    "param5","param6","param7","param8","param9","param10","message")
        choices = (
                    (1,'Unsatisfactory'),
                    (2,'Satisfactory'),
                    (3,'Good'),
                    (4,'Very Good'),
                    (5,'Outstanding')
                    )
        widgets = {
                'param1':forms.TypedChoiceField(choices=choices, widget=forms.RadioSelect, coerce=int),
                'message': forms.Textarea(attrs={'cols': 80, 'rows': 20}),
        }

--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@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/CAMO5YApwyjBSZgFEKHc6F0c1NVJCwdPkpnr%3DY%2B%3DV00hZsPSCcA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment