insert problem with mysql db

models.py

class AddSectors(models.Model):

    sectorName = models.CharField(max_length=200)

    uDate = models.DateTimeField()

# only  admin can add Category name with sectors name

class AddCategory(models.Model):

    sectorId = models.ForeignKey(AddSectors, on_delete=models.CASCADE, null=True)

    categoryName = models.CharField(max_length=200)

    date = models.DateTimeField()

views.py

if 'addCategoryBtn' in request.POST:

        inputCategoryName = strip_tags(request.POST.get("CategoryName"))

        inputSelectSectorId = strip_tags(request.POST.get("selectSectorId"))


        addCategoryQuery = AddCategory(sectorId = inputSelectSectorId , categoryName = inputCategoryName,  date = datetime.today() )

        addCategoryQuery.save()

        messages.success(request, 'Sector Added Successfully')

 showAllSector = AddSectors.objects.all()
 dataArray = {
        'showAllSectorss': showAllSector
}
return render(request, "adminTemplates/addSectorCategory.html", dataArray )

addSectorCategory.html

<div class="col-6">
<select class="form-select form-select-sm" name="selectSectorId" >
<option selected>Sectors Name</option>
 {% for forSector in showAllSectorss %}
     <option value="{{forSector.id}}">{{ forSector.sectorName }}</option>
 {% endfor %} 
</select>
</div>

and then when i select sector and type category name pressed the button got this error

ValueError at /AddSectorCategory

Cannot assign “‘1’”: “AddCategory.sectorId” must be a “AddSectors” instance.

  • D:\11 august 2021\workspace\python django projects\reffral\reff\home\views.py , line 521, in adminAddSectorCategory
  1. addCategoryQuery = AddCategory(sectorId = inputSelectSectorId , categoryName = inputCategoryName, date = datetime.today() )

How to Solve this problem sir

You can use the foreign key field name (sectorId_id) rather than the foreign key (sectorId) in the constructor at that line.

problem solved thanks sir