Cannot insert PSOT data in SQLlite3 db in Django – NOT NULL constraint failed error

I have a page with 10 questions and a comment field. The questions’ answers are selected via are radio buttons, the comment is a text field. When I tr to get the POST data entered into my SQL data base I get the error:

NOT NULL constraint failed: ISO22301_answer.value

The SQL table has 3 fields:

Name – an identifier from LQ1 to LQ10

Answer – integer from 0 – 5

Question – a FK that has the text of the question being answered. Since the questions are in teh db when I g to admin they show up as a drop down in answer.

The relevant file snippets are:

models.py

class Topic(models.Model):
    name = models.CharField(max_length=30)

    def __str__(self):
        return f"{self.name}"


class Question(models.Model):
    question = models.CharField(max_length=200)
    name = models.CharField(max_length=20)  # THis is probably useless

    topic = models.ForeignKey(
        Topic, on_delete=models.CASCADE
    )  # Django will store this as topic_id on database

class Answer(models.Model):
    value = models.IntegerField()
    name = models.CharField(max_length=20)
    # q_id = models.CharField(max_length=10, default="XX")  # USed foreignkey instead
    question = models.ForeignKey(
        Question, on_delete=models.CASCADE
    )


    def __str__(self):
        return f"{self.question} value is {self.value}"

leadership.html (one of the 10 rows):

  <td colspan="7">Risk Assessment and Planning</td>
  <tr>
      <td class="question">Conducts annual risk assesments and identifies key risks and mitigation actions</td>
      <td>
        <input  name="LQ1" type="radio" value="1" id="LQ1_1" />1
      </td>
      <td>
        <input name="LQ1" type="radio" value="2" id="LQ1_2" />2
      </td>
      <td>
        <input name="LQ1" type="radio" value="3"  id="LQ1_3"/>3
      </td>
      <td>
        <input name="LQ1" type="radio" value="4"  id="LQ1_4"/>4
      </td>
      <td>
        <input name="LQ1" type="radio" value="5"  id="LQ1_5"/>5
    </td>
    <td>
      <input name="LQ1" type="radio" value="0"  id="LQ1_6"/>
    </td>
  </tr>

views.py:

def leadership(request):
    if request.method == "POST":
        results=dict(request.POST) # get POST data from form
        print("Post", results)
        name = request.POST.get('name')
        value =  request.POST.get('value')
        answer = Answer(name=name, value=value)
        answer.save()
        # everything belwo here is just to calculate a score and render an anser page with color coding based on score
        score_comments=create_list(results) #Get tuple with (score, comments)
        score=score_comments[0]
        comments=score_comments[1]
        question_answers = []
        question_answers =  score_comments[2]
        results_color = score_color(score)
        print(score, results_color,comments, question_answers)
        #answers(results)
        context = {
            "shade": results_color
        }
        return render(request, "leadershipresults.html", context)
    else:
        return render(request, "leadership.html")

POST Data returned:


Variable        Value
csrfmiddlewaretoken 'mR6idvTRbovOc1V24ZcyJsosXaylGQx4SZ4f99BlAQ2gvJkwK7LfhxMJMYjr3bAo'
LQ1         '2'
LQ2         '2'
LQ3         '2'
LQ4         '2'
LQ5         '2'
LQ6         '2'
LQ7         '2'
LQ8         '2'
LQ9         '2'
LQ10            '2'
L_Text          'nmbvbncvbcxv'

SQL tables

ISO22301_answer             auth_user                 
ISO22301_area               auth_user_groups          
ISO22301_comment            auth_user_user_permissions
ISO22301_question           django_admin_log          
ISO22301_topic              django_content_type       
auth_group                  django_migrations         
auth_group_permissions      django_session            
auth_permission

sqlite> PRAGMA table_info(ISO22301_answer);
0|id|INTEGER|1||1
1|value|INTEGER|1||0
2|name|varchar(20)|1||0
3|question_id|bigint|1||0

I tried removing the Token and Comments from the returned POST data and the doing get() but that didn’t work.

I used pop and del to do that.

I also tried adding “blank=True, null=True” to

class Answer(models.Model):
    value = models.IntegerField()
    name = models.CharField(max_length=20)
    # q_id = models.CharField(max_length=10, default="XX")  # USed foreignkey instead
    question = models.ForeignKey(
        Question, on_delete=models.CASCADE, blank=True, null=True)

to no avail.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật