I am trying to insert data into a table using django form but when i try to save the values i keep getting an error that the object has no attribute “…”
forms.py
class inflowForm(forms.ModelForm):
class Meta:
model = poItem
fields = ['expiryDate', 'added_quantity']
expiryDate = forms.DateField(
widget=DateInput
)
views.py
def inflowA2(request, pk):
page = "inflowA2"
employee = Employee.objects.get(uuid = request.user.uuid)
today = date.today()
orderNo = po.objects.get(number = pk)
orderItems = poItem.objects.filter(po__number = pk)
today = date.today()
form = inflowForm()
if request.method == "POST":
form= inflowForm(request.POST)
if form.is_valid():
form.save(commit=False)
for x in orderItems:
if x.item.category == "مُجْمِعٌ":
form.expiryDate = x.item.expiryDate
form.added_quantity += x.item.quantity
form.save()
# return HttpResponseRedirect (reverse ("inflowA2", args=[request.POST['number']]))
context = {
'page': page,
'employee': employee,
'date': today,
'po': orderNo,
'items':orderItems,
'form': form
}
return render(request, "forms/warehouse/inflow/a2.html", context)
models.py
class po(models.Model):
uuid = models.UUIDField(primary_key=True,default=uuid.uuid4, editable=False, max_length=36)
number = models.IntegerField(auto_created=True)
employee = models.ForeignKey(Employee, on_delete=models.CASCADE)
supplier = models.ForeignKey(Supplier, on_delete=models.CASCADE)
warehouse = models.ForeignKey(Warehouse, on_delete=models.CASCADE)
completed = models.BooleanField(default=False)
po_created_at = models.DateTimeField(auto_now_add=True)
def __str__(self):
return str(self.uuid)
class poItem(models.Model):
uuid = models.UUIDField(primary_key=True,default=uuid.uuid4, editable=False, max_length=36)
item = models.ForeignKey(Item, on_delete=models.CASCADE, related_name='items')
po = models.ForeignKey(po, on_delete=models.CASCADE, related_name='poitems')
required_quantity = models.IntegerField()
added_quantity = models.IntegerField()
expiryDate = models.DateField(null=True, blank=True)
class Meta:
ordering = ('item',)
def __repr__(self):
return self.item.description
class Item(models.Model):
uuid = models.UUIDField(primary_key=True,default=uuid.uuid4, editable=False, max_length=36)
# name = models.CharField(max_length=100)
description = models.TextField()
expiryDate = models.DateField(null=True, blank=True)
quantity = models.IntegerField()
category_choices = (
('مستوي واحد','مستوي واحد'),
('مُجْمِعٌ','مُجْمِعٌ'),
)
category = models.CharField(
max_length=15,
choices=category_choices
)
unit_choices = (
('صندوق','صندوق'),
('متر','متر'),
('عدد','عدد'),
)
unit = models.CharField(
max_length=15,
choices=unit_choices
)
def __str__(self):
return self.description
html file
<form action="{% url "inflowA2" po.number %}" method="post">
{% csrf_token %}
<tbody>
{% for x in items %}
<tr class="table-text position">
<td>{{x.item.description}}</td>
<td>{{x.item.category}}</td>
{% if x.item.category == "مُجْمِعٌ" %}
<td>{{form.expiryDate}}</td>
{% else %}
<td>-</td>
{% endif %}
<td>{{x.item.unit}}</td>
<td>{{x.required_quantity}}</td>
<td>{{x.item.quantity}}</td>
<td>{{form.added_quantity}}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
<div>
<button class="button button-search">تأكيد</button>
</div>
</form>