JavaScript
x
23
23
1
**The error posted is Integrity Error 1048 column cannot be null**
2
IntegrityError at /Supply/supplyItems
3
(1048, "Column 'item_id_id' cannot be null")
4
Request Method: POST
5
Request URL: http://127.0.0.1:8000/Supply/supplyItems
6
Django Version: 4.1.1
7
Exception Type: IntegrityError
8
Exception Value:
9
(1048, "Column 'item_id_id' cannot be null")
10
Exception Location: C:Users63966AppDataLocalProgramsPythonPython310libsite-packagesdjangodbbackendsmysqlbase.py, line 80, in execute
11
Raised during: Supply.views.SupplyItem
12
Python Executable: C:Users63966AppDataLocalProgramsPythonPython310python.exe
13
Python Version: 3.10.6
14
Python Path:
15
['D:\CIT\3rd Year\IM 2\New '
16
'folder\sarisaristoreproject\sarisaristoreproject\sarisaristoreproject',
17
'C:\Users\63966\AppData\Local\Programs\Python\Python310\python310.zip',
18
'C:\Users\63966\AppData\Local\Programs\Python\Python310\DLLs',
19
'C:\Users\63966\AppData\Local\Programs\Python\Python310\lib',
20
'C:\Users\63966\AppData\Local\Programs\Python\Python310',
21
'C:\Users\63966\AppData\Local\Programs\Python\Python310\lib\site-packages']
22
Server time: Sat, 15 Oct 2022 07:25:53 +0000
23
Here is my models.py
JavaScript
1
14
14
1
class Supplier(models.Model):
2
supplier_id = models.AutoField(primary_key=True)
3
company_name = models.CharField(max_length=20, null=False)
4
company_address = models.CharField(max_length=50, null=False)
5
company_num = models.IntegerField(null=False)
6
7
def __str__(self):
8
return str(self.company_name)
9
10
class supplyItem(models.Model):
11
item_id = models.ForeignKey('Items.Items', on_delete=models.CASCADE)
12
supplier_id = models.ForeignKey(Supplier, on_delete=models.CASCADE)
13
numOfItemsS = models.IntegerField()
14
Items model from different App:
JavaScript
1
11
11
1
class Items(models.Model):
2
item_id = models.AutoField(primary_key=True)
3
item_name = models.CharField(max_length=50, null=False)
4
item_qty = models.IntegerField(null=False)
5
item_prc = models.FloatField(null=False)
6
purchase = models.ManyToManyField('registration.Customer', through='Purchase.purchasedItem', related_name='items', blank=True)
7
sell = models.ManyToManyField('registration.Employee', through='Sell.sellItem', related_name='items', blank=True)
8
9
def __str__(self):
10
return self.item_name
11
My views.py
JavaScript
1
19
19
1
class HomeView(View):
2
template = 'supply.html'
3
4
def get(self, request):
5
return render(request, self.template)
6
7
class SupplyItem(View):
8
template = 'SupplyItems.html'
9
10
def get(self, request):
11
form = SupplyItemForm()
12
return render(request, self.template, {'form': form})
13
14
def post(self, request):
15
form = SupplyItemForm(request.POST)
16
if form.is_valid():
17
form.save()
18
return render(request, self.template, {'form': form})
19
Forms.py
JavaScript
1
31
31
1
class SupplyItemForm(ModelForm):
2
itemID = forms.ModelChoiceField(widget=forms.Select(), queryset=Items.objects.only('item_id'), label='Item name')
3
supplierID = forms.ModelChoiceField(widget=forms.Select(), queryset=Supplier.objects.only('supplier_id'), label='Supplier Company')
4
itemQty = forms.IntegerField(widget=forms.NumberInput, label='Quantity')
5
6
def __init__(self, *args, **kwargs): # constructor
7
super(SupplyItemForm, self).__init__(*args, **kwargs)
8
9
class Meta:
10
model = supplyItem
11
fields = ['itemQty', 'itemID', 'supplierID']
12
13
def clean_itemQty(self):
14
qty = self.data.get('itemQty')
15
if int(qty) <= 0:
16
raise ValidationError('Quantity must be a positive number!')
17
else:
18
return qty
19
20
def clean_itemID(self):
21
data = self.cleaned_data.get('itemID')
22
if data == '':
23
raise ValidationError('Please select a value!')
24
return data
25
26
def clean_supplierID(self):
27
data = self.cleaned_data.get('supplierID')
28
if data == '':
29
raise ValidationError('Please select a value!')
30
return data
31
My template
JavaScript
1
18
18
1
<!DOCTYPE html>
2
<html lang="en">
3
<head>
4
<meta charset="UTF-8">
5
<title>Supply Item</title>
6
</head>
7
<body>
8
<h1>Supply Store Items</h1>
9
<h2>Select Supplier and Items to Supply</h2>
10
<hr/>
11
<form method="post">
12
{% csrf_token %}
13
{{form.as_p}}
14
<input type="submit" value="Add Store items">
15
</form>
16
</body>
17
</html>
18
And the form when i run the server is this:
Advertisement
Answer
In you model the field is named item_id
but in your ModelForm
the field is named itemID
. The field names should be the same.
Rename the fields in your ModelForm
so they match the Model
.