Click here to Skip to main content
15,664,273 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 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(
        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():

           return HttpResponseRedirect('/restock') 

        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
        form = LocationStateform()

    return render(request, 'form2.html', {'form3': form3})    

and this was my html template:

<!DOCTYPE html>
<html lang="en">
<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() {

<body onload="onLoadSubmit()">

<form  form name="sebform" id="sebform" method="post" >
    {% csrf_token %}
    {{ form2 }}
<input type="name" />

<form  form name="sebform3" id="sebform3" method="post" >
    {% csrf_token %}
    {{ form3 }}
<input type="name" />


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),
Updated 18-Jul-22 22:56pm

1 solution

location = models.ForeignKey('Location', on_delete=models.RESTRICT, null=True)

The foreign key should link to the actual model class name, rather than a string.
Try Location without the quote characters:
location = models.ForeignKey(Location, on_delete=models.RESTRICT, null=True)

As to the actual error message, you need to explain where it occurs. Just dumping all the code and expecting us to figure out what is happening when you run it is not a good idea.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900