1

This is very strange to me. I am setting a breakpoint inside of a Django signal receiver create_customer_settings to debug this. The app is throwing a ValueError even though the new CustomerSettings object is successfully created. When I try to create it for a second time, I get a UNIQUE constraint error, but that is because it was already added successfully.

Why the ValueError when isinstance(this_user, User) == True?

Is this an error I can safely catch and ignore? Is it a problem because the CustomerSettings id is different than the User id?

create_customer_settings

@receiver(post_save, sender=User)
def create_customer_settings(sender, instance, created, **kwargs):
    if created:
      this_user = User.objects.get(id=instance.id)
      pdb.set_trace()
      CustomerSettings.objects.create(
          user=this_user, 
          email=this_user.email, 
          date_of_birth="1998-01-04", 
          in_private_mode=False
    )                                 

pdb.set_trace() output 1:

System check identified no issues (0 silenced).
October 29, 2018 - 23:42:57
Django version 2.1.1, using settings 'myapp.settings'
Starting development server at http://127.0.0.1:7000/
Quit the server with CONTROL-C.
> /src/myapp/myapp/models.py(36)create_customer_settings()
-> pdb.set_trace()
(Pdb) this_user
<User: new_user_8080>
(Pdb) isinstance(this_user, User)
True
(Pdb) CustomerSettings.objects.create(
    user=this_user, 
    email=this_user.email, 
    date_of_birth="1998-01-04", 
    in_private_mode=False
)                                 
*** ValueError: Cannot assign 
"<CustomerSettings: CustomerSettings object (6)>": 
"CustomerSettings.user" must be a "User" instance.

pdb.set_trace() output 2:

(Pdb) this_user.is_anonymous
False
(Pdb) this_user
<User: new_user_8080>
(Pdb) CustomerSettings.objects.create(
    user=this_user, 
    email=this_user.email, 
    date_of_birth="1998-01-04", 
    in_private_mode=False
)                                 
*** django.db.utils.IntegrityError: 
UNIQUE constraint failed: myapp_customersettings.user_id
(Pdb) CustomerSettings.objects.all()
<QuerySet [<CustomerSettings: CustomerSettings object (1)>,
<CustomerSettings: CustomerSettings object (2)>,
<CustomerSettings: CustomerSettings object (3)>,
<CustomerSettings: CustomerSettings object (4)>,
<CustomerSettings: CustomerSettings object (5)>,
<CustomerSettings: CustomerSettings object (6)>]>
(Pdb) CustomerSettings.objects.all().values()
<QuerySet [
{'id': 1, 'user_id': 1, 'timezone': 'America/New_York', 'email':...
    ...
    ...
    ... 
, {'id': 6, 'user_id': 15, 'timezone': '', 'email': '[email protected]', 
'email_confirmed': False, 'date_of_birth': datetime.date(1998, 1, 4), 'first_name': '', 'last_name': '', 'in_private_mode': False}]>
(Pdb) CustomerSettings.objects.filter(user_id=15)
<QuerySet [<CustomerSettings: CustomerSettings object (6)>]>
(Pdb) CustomerSettings.objects.filter(user_id=15).values()
<QuerySet [{'id': 6, 'user_id': 15, 'timezone': '', 'email': '[email protected]', 'email_confirmed': False, 'date_of_birth': datetime.date(1998, 1, 4), 'first_name': '', 'last_name': '', 'in_private_mode': False}]>
(Pdb) CustomerSettings.objects.filter(user_id=15).values()[0]
{'id': 6, 'user_id': 15, 'timezone': '', 'email': '[email protected]', 'email_confirmed': False, 'date_of_birth': datetime.date(1998, 1, 4), 'first_name': '', 'last_name': '', 'in_private_mode': False}
(Pdb) CustomerSettings.objects.filter(user_id=15).values()[0]['id']
6
(Pdb) User.objects.get(id=15)
<User: new_user_8080>
(Pdb) 

CustomerSettings Model:

class CustomerSettings(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE, unique=True, null=False, blank=False)
    timezone = models.CharField(max_length=50, choices=TIMEZONE_CHOICES, blank=False, null=False)
    email = models.EmailField(
            verbose_name='email address',
            max_length=255, blank=False, null=False,
            unique=True)
    email_confirmed = models.BooleanField(default=False)
    date_of_birth = models.DateField()
    first_name = models.CharField(max_length=50, null=False, blank=False)
    last_name = models.CharField(max_length=50, null=False, blank=False)
    in_private_mode = models.BooleanField()
5
  • i think you are trying to create a CustomerSettings just after instance created, try to direct assign CustomerSettings.objects.create( user=instance, email=instance.email, date_of_birth="1998-01-04", in_private_mode=False ) Commented Oct 30, 2018 at 1:10
  • To be clear, the sender is user. I got an error trying to assign directly. I'll try again tomorrow. Commented Oct 30, 2018 at 2:46
  • Have you got a custom User class perhaps? Are you sure the OneToOneField references the same User class that you have in that signal? Commented Oct 30, 2018 at 8:04
  • Yes, I did have a custom user class previously built that was floating around in models, but was not used in the signal. I commented it out, and I'm still getting the error: Exception Type: ValueError Exception Value: Cannot assign "<CustomerSettings: CustomerSettings object (11)>": "CustomerSettings.user" must be a "User" instance. Commented Oct 30, 2018 at 10:42
  • @PankajSharma - This did not work either. (Pdb) this_user <User: new_user_2020> (Pdb) this_user[0] *** TypeError: 'User' object does not support indexing (Pdb) this_user == instance True (Pdb) CustomerSettings.objects.create(user=instance, email=instance.email, date_of_birth="1998-01-04", in_private_mode=False) *** ValueError: Cannot assign "<CustomerSettings: CustomerSettings object (13)>": "CustomerSettings.user" must be a "User" instance. (Pdb) Commented Oct 30, 2018 at 10:46

2 Answers 2

1

Double check that the code you posted is what you have running. The error message ValueError: Cannot assign "<CustomerSettings: CustomerSettings object (6)>": "CustomerSettings.user" must be a "User" instance means that you're attempting to create a CustomerSettings instance where the user property has the instance of CustomerSettings that's id is 6.

My guess is there's a disconnect between what you have running and what you posted. If there isn't, then something is calling create_customer_settings where the instance argument being an instance of CustomerSettings.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. Code is direct from console, and I made sure to double check not to waste anyone's time. Appreciate the answer
Made sure to include this: -> pdb.set_trace() (Pdb) this_user <User: new_user_8080> (Pdb) isinstance(this_user, User) True
0

I took a long and hard look at the traceback. A method that was working previously, for update_customer_settings, was being called after the successful creation of the CustomerSettings model after the post_save event by User, which in that case the instance was indeed CustomerSettings.

I apologize for the confusion. You were all pointing me in the right direction, thank you. I need to handle this in a much different way.

traceback

File "/Users/my_folder/my_app/my_app/models.py", line 44, in update_customer_settings
CustomerSettings.objects.create(user=instance)
ValueError: Cannot assign "<CustomerSettings: CustomerSettings object (14)>": "CustomerSettings.user" must be a "User" instance.

update_customer_settings

@receiver(post_save, sender=CustomerSettings)
def update_customer_settings(sender, instance, created, **kwargs):
    if created:
        CustomerSettings.objects.create(user=instance)
    instance.customersettings.save()

1 Comment

Glad you were able to figure it out!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.