# insert problem with mysql db

**URL:** <https://forum.djangoproject.com/t/insert-problem-with-mysql-db/10317>\
**Category:** Using Django\
**Created:** [October 30, 2021, 5:30pm UTC](https://forum.djangoproject.com/t/insert-problem-with-mysql-db/10317 "2021-10-30T17:30:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ankur520](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/ankur520/32/6170_2.png) [@ankur520](https://forum.djangoproject.com/u/ankur520)\
**Post date:** [October 30, 2021, 5:30pm UTC](https://forum.djangoproject.com/t/insert-problem-with-mysql-db/10317/1 "2021-10-30T17:30:12Z")

</div>

**models.py**

```auto
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**

```auto
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**

```auto
<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**

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [October 30, 2021, 11:07pm UTC](https://forum.djangoproject.com/t/insert-problem-with-mysql-db/10317/2 "2021-10-30T23:07:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ankur520](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/ankur520/32/6170_2.png) [@ankur520](https://forum.djangoproject.com/u/ankur520)\
**Post date:** [October 31, 2021, 11:12am UTC](https://forum.djangoproject.com/t/insert-problem-with-mysql-db/10317/3 "2021-10-31T11:12:34Z")

</div>

problem solved thanks sir
