0

I am trying to create an order object and assign the order's customer field (which is a foreign key for User) to the current user. Even though it passes the if statement and it can authenticate the request.user, it assigns customer as a blank value.

When I print customer it prints a blank line, however when I print customer.id, it prints the ID of the customer. This happens in all views in the project so its a global issue

I belive this has something to do with the fact that my user class is inheriting from AbstractUser

utils.py

def cartData(request):
    if request.user.is_authenticated:
        print('This will print')
        customer = request.user
        print(customer) #This will print as a blank line
        print(customer.id) #This will print the request.user.id
        print(customer.username) #This will print the request.user.username
        order, created = Order.objects.get_or_create(customer=customer, complete=False)

models.py

from django.db import models
from django.contrib.auth.models import AbstractUser

class User(AbstractUser):
   author = models.BooleanField(default=False)

settings.py

AUTH_USER_MODEL = 'main.User' 

The database is updated with the user as blank too. I feel like this may be an easy one but I can't figure it out! Thanks.

sthompson232
  • 52
  • 1
  • 7
  • Have you implemented `__str__` or `__repr__` on your custom user class? Make sure to return a proper string representation there (you might be returning an empty string there). Your Order instance is most likely created properly without issues. The variable customer is not a blank value as you say, it's just that when you print it it's string representation is an empty string. – Abdul Aziz Barkat Feb 15 '21 at 12:55
  • What is `username` field value? – unknown Feb 15 '21 at 12:59
  • @AbdulAzizBarkat This hasn't seemed to work unfortunately. It's also blank on the database so I don't believe its just a representational problem (I have on_delete set to null). Thanks for the suggestion. – sthompson232 Feb 15 '21 at 13:15
  • @unknown username value prints off correctly too. – sthompson232 Feb 15 '21 at 13:18
  • @sthompson232 Can you show `print(customer.username)`? – unknown Feb 15 '21 at 13:20
  • @unknown what do you mean sorry? `print(customer.username)` will print off the current user's username like it should normally do. – sthompson232 Feb 15 '21 at 13:31
  • @sthompson232 This is strange because the `AbstractUser` inherits from the `AbstractBaseUser`, who already has a `__str__` method that outputs the user's username. Of course, you can try to rewrite the `__str__` method in your model ... But it's very strange. – unknown Feb 15 '21 at 13:36
  • Which version of Django you are use? – unknown Feb 15 '21 at 13:37
  • 1
    @sthompson232 What does `print(order.customer is None)` give you? Also try `print(type(customer))`, also `print(str(customer).strip() == "")` – Abdul Aziz Barkat Feb 15 '21 at 13:41
  • @sthompson232 can you show list of all fields of `User` model? – unknown Feb 15 '21 at 13:51
  • The User model is already displayed in the question. I only have one extra field added to it! – sthompson232 Feb 15 '21 at 13:55
  • django version 3.1.6 – sthompson232 Feb 15 '21 at 13:56

1 Answers1

0

I have now added first_name and last_name to the user and it works. Still a little confused though as I did not set __str__ or __repr__ to first or last name on the user model. If anyone knows why this is the case I would be interested to know. Thanks for al the help!

sthompson232
  • 52
  • 1
  • 7