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.
You need to change the value field of your answer model.
class Answer(models.Model):
value = models.IntegerField()
=>
value = models.IntegerField(null=True)
OR
value = models.IntegerField(default=984354684)