ValueError at /borrow/

I am tryin to automatically populate a table B whenever a user fills out a form that populate tableA. Whenever i fill out the form to populate table A, i run into this error.

Traceback (most recent call last):
  File "/home/dubsy/virtualenvs/djangoproject/lib/python3.9/site-packages/django/core/handlers/exception.py", line 55, in inner
    response = get_response(request)
  File "/home/dubsy/virtualenvs/djangoproject/lib/python3.9/site-packages/django/core/handlers/base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/dubsy/virtualenvs/djangoproject/libmain/books/views.py", line 21, in borrow
    borrower.save()
  File "/home/dubsy/virtualenvs/djangoproject/lib/python3.9/site-packages/django/db/models/base.py", line 812, in save
    self.save_base(
  File "/home/dubsy/virtualenvs/djangoproject/lib/python3.9/site-packages/django/db/models/base.py", line 878, in save_base
    post_save.send(
  File "/home/dubsy/virtualenvs/djangoproject/lib/python3.9/site-packages/django/dispatch/dispatcher.py", line 176, in send
    return [
  File "/home/dubsy/virtualenvs/djangoproject/lib/python3.9/site-packages/django/dispatch/dispatcher.py", line 177, in <listcomp>
    (receiver, receiver(signal=self, sender=sender, **named))
  File "/home/dubsy/virtualenvs/djangoproject/libmain/books/models.py", line 84, in create_lending
    ApprovedLending.objects.create(member=row["member_id"])
  File "/home/dubsy/virtualenvs/djangoproject/lib/python3.9/site-packages/django/db/models/manager.py", line 85, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/home/dubsy/virtualenvs/djangoproject/lib/python3.9/site-packages/django/db/models/query.py", line 669, in create
    obj = self.model(**kwargs)
  File "/home/dubsy/virtualenvs/djangoproject/lib/python3.9/site-packages/django/db/models/base.py", line 541, in __init__
    _setattr(self, field.name, rel_obj)
  File "/home/dubsy/virtualenvs/djangoproject/lib/python3.9/site-packages/django/db/models/fields/related_descriptors.py", line 235, in __set__
    raise ValueError(

Exception Type: ValueError at /borrow/
Exception Value: Cannot assign "1": "ApprovedLending.member" must be a "User" instance.

Here is my models.py

class A(models.Model):
    member = models.ForeignKey(User, on_delete=models.CASCADE, default="")
    book = models.ForeignKey(Books, on_delete=models.CASCADE, default="")
    library_no = models.CharField(default="", max_length=255, blank=True)
    staff_id = models.CharField(default="", max_length=255, blank=True)
    application_date = models.DateTimeField(auto_now_add=True)

class B(models.Model):
    member = models.ForeignKey(User, on_delete=models.CASCADE, default="", null=True)
    book = models.ForeignKey(Books, on_delete=models.CASCADE, default="", null=True)
    approved = models.BooleanField()
    approval_date = models.DateTimeField(auto_now_add=True, null=True)

here is the function i used to automatically populate table B when the form for table A has been filled out.

@receiver(post_save, sender=BorrowBook)
def create_lending(sender, instance,**kwargs):
    data = BorrowBook.objects.values()
    for row in data:
        ApprovedLending.objects.create(member=row["member_id"])

How do i solve this issues.

As the error says: Exception Value: Cannot assign "1": "ApprovedLending.member" must be a "User" instance.

You are trying to assign the ID directly:

...
ApprovedLending.objects.create(member=row["member_id"])
...

Instead you should retrieve the User (member) instance:

...
member = User.objects.get(id=row["member_id"])
ApprovedLending.objects.create(member=member)
...
Back to Top