-1

I try to Insert Foreign Key into the database. But When I hit the submit, I get

Error:MultiValueDictKeyError

model.py

from django.db import models

class Candidate_Military(models.Model):
    """docstring forCandidate_Military."""
    auto_increment_id = models.AutoField(primary_key=True)
    military_status = models.CharField(max_length=100,null=True)
    military_reason = models.CharField(max_length=250,null=True)

    def __int__(self):
        return self.auto_increment_id

class Candidate_Basic(models.Model):
    """docstring for Candidate_basic."""
    id_number = models.CharField(primary_key=True,max_length=13)
    position = models.CharField(max_length=250)
    salary = models.IntegerField()
    profile_pic = models.ImageField()
    nickname = models.CharField(max_length=100)
    name_title = models.CharField(max_length=50)
    firstname = models.CharField(max_length=250)
    lastname = models.CharField(max_length=250)
    candidate_military = models.ForeignKey(Candidate_Military, on_delete=models.CASCADE,null=True)
    def __int__(self):
        return self.id_number

view.py

from .models import Candidate_Basic, Candidate_Military

def index(request):
    template=loader.get_template("index.html")
    return HttpResponse(template.render())

def submit_applyjob(request):
    print("ohh! It's sumbitted!")

    military_status = request.POST.get('military_status')
    military_reason = request.POST.get('military_reason')

    candidate_military = Candidate_Military(military_status=military_status,military_reason=military_reason)
    candidate_military.save()

    id_number = request.POST["id_number"]
    position = request.POST["position"]
    salary = request.POST["salary"]
    profile_pic = request.POST["profile_pic"]
    nickname = request.POST["nickname"]
    name_title = request.POST["name_title"]
    firstname = request.POST["firstname"]
    lastname = request.POST["lastname"]
    print("candidate_military" + "-->>>" + str(candidate_military))
    candidate_military = request.POST["candidate_military"]

    candidate_basic = Candidate_Basic(id_number=id_number,position=position,salary=salary,
    profile_pic=profile_pic,nickname=nickname,name_title=name_title,
    firstname=firstname,lastname=lastname,candidate_military=candidate_military)
    candidate_basic.save()

    return render(request, "index.html")

And When I fill the form and hit the submit button It's Error Like This

enter image description here

I don't Understand Why It cannot insert into my database. I try to print the value of 'candidate_military' It's Print the right value!

enter image description here

Plz Help me to Debug This issue T^T

I try to fix this code with

candidate_military = request.POST.get("candidate_military",False)

But It's not Work ;

ValueError: Cannot assign "False": "Candidate_Basic.candidate_military" must be a "Candidate_Military" instance.

Daniel
  • 10,641
  • 12
  • 47
  • 85
Fah Nateecha
  • 151
  • 3
  • 14
  • Possible duplicate of [django MultiValueDictKeyError error, how do I deal with it](https://stackoverflow.com/questions/5895588/django-multivaluedictkeyerror-error-how-do-i-deal-with-it) – Red Cricket Aug 26 '18 at 04:14
  • It's not work for my code, I edit already but value error ValueError: Cannot assign "False": "Candidate_Basic.candidate_military" must be a "Candidate_Military" instance. – Fah Nateecha Aug 26 '18 at 05:01

1 Answers1

1

The issue is in this line

candidate_military = request.POST["candidate_military"]

There is no need for this line. Just remove it and the code should just work fine.

Chai
  • 396
  • 2
  • 5