I created a model called Location and another model called Replenishment_Tasks. Replenishment_Tasks has a field called location that is a one-to-many field that is connected to the Location model, so that there is may be more Replenishment_Task records that correspond to a single Location records , but not vice versa.
I have created a form ( using model form) that is supposd to create a record in the
Replenishment_Tasks model every time the page it is on will load.
Whevever I load the page the form is on, Django says
ValueError: Cannot assign "'09-11-24-C-02-01'": "Replenishment_Task.location" must be a "Location" instance.
Is there any way I can work around this issue?
Any help is appreciated.
PS. I am new to Django and programming in general, so please excuse me if my language may be a bit confusing
What I have tried:
I really have no idea what to try.
Here is my models.py fiile:
from django.db import models
class Location(models.Model):
location_in_location = models.CharField(max_length=100, null=True, help_text='where')
line = models.IntegerField(null = True)
area = models.IntegerField(null = True)
our_item_no = models.CharField(max_length=100,help_text='the quantity phisically on location', null=True)
vendor_item_no = models.CharField(max_length=100,help_text=' item used by vendor ', null=True)
barcode = models.CharField(max_length=100,help_text=' inner colli barcode ', null=True)
short_description = models.CharField(max_length=100,help_text='product brand yada yada', null=True)
remarks = models.CharField(max_length=100,help_text='the quantity phisically on location', null=True)
tuple5 = (
('c', 'coming'),
('d', 'defq'),
('r', 'refilled'),
('w', 'wrong'),
('e', 'empty'),
)
loc_state = models.CharField(
max_length=1,
choices=tuple5,
blank=True,
default='r',
help_text=' is the house: empty, refilled or empty because we are out of that prod?',
)
def __str__(self):
"""String for representing the Model object."""
return self.location_in_location
class Replenishment_Task(models.Model):
task_number = models.IntegerField(null = True)
emergency_lane = models.IntegerField(null = True)
location = models.ForeignKey('Location', on_delete=models.RESTRICT, null=True)
dummy = models.CharField(max_length=100,help_text=' just dummy ', null=True)
dummyinteger = models.IntegerField(null = True)
sebdate2 = models.DateTimeField(auto_now=True, auto_now_add=False, null=True, blank=True)
def __str__(self):
"""String for representing the Model object."""
return self.dummy
class Quantity_Adjustment_Task(models.Model):
reported_qty = models.CharField(max_length=100,help_text='the quantity phisically on location', null=True)
Normal_hero = models.CharField(max_length=100,help_text='the name of the one who sent the request', null=True)
reported_barcode = models.CharField(max_length=100,help_text='the barcode on the product,as reported by the hero', null=True)
breakage = models.CharField(max_length=100,help_text='number of broken colli', null=True)
sebdate = models.DateTimeField(auto_now=True, auto_now_add=False, null=True, blank=True)
anotherone = models.CharField(max_length=100,help_text='smth', null=True)
def __str__(self):
"""String for representing the Model object."""
return self.reported_qty
And this is my views file:
from django.shortcuts import render
def index(request):
return render(request, 'index.html')
import datetime
from django.shortcuts import render
from django.http import HttpResponse
from django.http import HttpResponseRedirect
from .forms import Replenishment_Taskform, LocationStateform
def Replenishment_Taskview(request):
if request.method == 'POST':
form = Replenishment_Taskform(request.POST)
if form.is_valid():
form.save()
return HttpResponseRedirect('/restock')
else:
form = Replenishment_Taskform()
return render(request, 'form2.html', {'form2': form})
def Locationview(request):
if request.method == 'POST':
form3 = LocationStateform(request.POST)
if form3.is_valid():
return HttpResponseRedirect('/restock/')
# if a GET (or any other method) we'll create a blank form
else:
form = LocationStateform()
return render(request, 'form2.html', {'form3': form3})
and this was my html template:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<h1>time when page loaded</h1>
<p1 id="valtimewhenloaded"></p1>
<script language="javascript">
let callcount = 0;
function onLoadSubmit() {
document.sebform.submit();
document.sebform3.submit();
}
</script>
</head>
<body onload="onLoadSubmit()">
<form form name="sebform" id="sebform" method="post" >
{% csrf_token %}
{{ form2 }}
<input type="name" />
</form>
<form form name="sebform3" id="sebform3" method="post" >
{% csrf_token %}
{{ form3 }}
<input type="name" />
</form>
</body>
</html>
and this was the urld file:
from django.urls import path
from django.urls.conf import include
from . import views
urlpatterns = [
path('', views.index, name='index'),
path('Replenishment_Task', views.Replenishment_Taskview),
path('Replenishment_Task', views.Locationview),
]