#Cannot assign "(<Employee: admin>,)": "Product.updateByStaffId" must be a "Employee" instance.

1 messages · Page 1 of 1 (latest)

light birch
#

Here is part of my code

class Product(models.Model):
  updateByStaffId = models.ForeignKey(Employee, on_delete=models.CASCADE, related_name='allEditedProducts')

class Employee(AbstractUser):
    username = models.CharField(max_length=30, unique=False)
    REQUIRED_FIELDS = ["username", "email"]
    def __str__(self):
         return self.username
    class Meta:
        db_table = 'employees'

And I try create a product

nowP =Product()
updateByStaffId = Employee.objects.get(id=request.user.id)
nowP.updateByStaffId = updateByStaffId

Then got error

Cannot assign "(<Employee: admin>,)": "Product.updateByStaffId" must be a "Employee" instance.

Anyone know how to fix it?

light birch
silver patio
#

Can you add the full error? It seems .get() returns a tuple but it is not possible. Maybe you should check if there is a comma at the end of the line.

light birch
# silver patio Can you add the full error? It seems .get() returns a tuple but it is not possib...

In console

Cannot assign "(<Employee: 1>,)": "Product.updateByStaffId" must be a "Employee" instance.
[27/Nov/2023 14:38:33] "POST /products/ HTTP/1.1" 200 3201

I think <Employee: 1> is from str

class Employee(AbstractUser):
    username = models.CharField(max_length=30, unique=False)
    REQUIRED_FIELDS = ["username", "email"]
    def __str__(self):
         return str(self.id)

By the way if

print(type(updateByStaffId))
<class 'staff.models.Employee'>
light birch
#

I am thinkin

updateByStaffId = Employee.objects.get(id=request.user.id)

nowP =Product(updateByStaffId = updateByStaffId # Will pass
# But
nowP =Product()
nowP.updateByStaffId = updateByStaffId # Will fail

Can some one tell me the reason :'(?
So confuse.

tight bronze
#

@light birchCan you post full code of your view? I have a suspicion of rogue comma

#

Also what do you think this lines do? Logically
updateByStaffId = Employee.objects.get(id=request.user.id)

light birch
tight bronze
#

(x,) looks like tuple