# Django Part 2, 10-Getting current user's profile

**URL:** https://forum.codewithmosh.com/t/django-part-2-10-getting-current-users-profile/17250
**Category:** Uncategorized
**Created:** [December 26, 2022, 5:09pm UTC](https://forum.codewithmosh.com/t/django-part-2-10-getting-current-users-profile/17250 "2022-12-26T17:09:04Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Trycos](https://avatars.discourse-cdn.com/v4/letter/t/eada6e/32.png) [@Trycos](https://forum.codewithmosh.com/u/Trycos)
#### Post date: [December 26, 2022, 5:09pm UTC](https://forum.codewithmosh.com/t/django-part-2-10-getting-current-users-profile/17250/1 "2022-12-26T17:09:04Z")

</div>

I have an issues with the error return 1048, “Column ‘user\_id’ cannot be null” and check the code in the finish lesson but still are not able to find the solution.

…/store/Serializers.py

class CustomerSerializer(serializers.ModelSerializer):  
user\_id = serializers.IntegerField(read\_only=True)

```
class Meta:
    model = Customer
    fields = ['id', 'user_id', 'phone', 'birth_date', 'membership']

```

…/store/views.py

class CustomerViewSet(CreateModelMixin, RetrieveModelMixin, UpdateModelMixin, GenericViewSet):  
queryset = Customer.objects.all()  
serializer\_class = CustomerSerializer

```
@action(detail=False, methods=['GET', 'PUT'])
def me(self, request):
    (customer, created) = Customer.objects.get_or_create(user_id=request.user.id)
    if request.method == 'GET':
        serializer = CustomerSerializer(customer)
        return Response(serializer.data)
    elif request.method == 'PUT':
        serializer = CustomerSerializer(customer, data=request.data)
        serializer.is_valid(raise_exception=True)
        serializer.save()
        return Response(serializer.data)

```

…/store/models.py

class Customer(models.Model):  
MEMBERSHIP\_BRONZE = ‘B’  
MEMBERSHIP\_SILVER = ‘S’  
MEMBERSHIP\_GOLD = ‘G’

```
MEMBERSHIP_CHOICES = [
    (MEMBERSHIP_BRONZE, 'Bronze'),
    (MEMBERSHIP_SILVER, 'Silver'),
    (MEMBERSHIP_GOLD, 'Gold'),
]
phone = models.CharField(max_length=255)
birth_date = models.DateField(null=True, blank=True)
membership = models.CharField(
    max_length=1, choices=MEMBERSHIP_CHOICES, default=MEMBERSHIP_BRONZE)
user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)

def __str__ (self):
    return f'{self.user.first_name} {self.user.last_name}'

@admin.display(ordering='user__first_name')
def first_name(self):
    return self.user.first_name

@admin.display(ordering='user__last_name')
def last_name(self):
    return self.user.last_name

class Meta:
    ordering = ['user __first_name', 'user__ last_name']

```

…/core/serializers.py  
class UserCreateSerializer(BaseUserCreateSerializer):  
class Meta(BaseUserCreateSerializer.Meta):  
fields = [‘id’, ‘username’, ‘password’, ‘email’, ‘first\_name’, ‘last\_name’]

class UserSerializer(BaseUserSerializer):  
class Meta(BaseUserSerializer.Meta):  
fields = [‘id’, ‘username’, ‘email’, ‘first\_name’, ‘last\_name’]

ERROR MESSAGE!!!  
Traceback (most recent call last):  
File “/Users/rogerlooser/.local/share/virtualenvs/storefront-JGOY8xqq/lib/python3.10/site-packages/django/core/handlers/exception.py”, line 55, in inner  
response = get\_response(request)  
File “/Users/rogerlooser/.local/share/virtualenvs/storefront-JGOY8xqq/lib/python3.10/site-packages/django/core/handlers/base.py”, line 197, in \_get\_response  
response = wrapped\_callback(request, \*callback\_args, \*\*callback\_kwargs)  
File “/Users/rogerlooser/.local/share/virtualenvs/storefront-JGOY8xqq/lib/python3.10/site-packages/django/views/decorators/csrf.py”, line 54, in wrapped\_view  
return view\_func(\*args, \*\*kwargs)  
File “/Users/rogerlooser/.local/share/virtualenvs/storefront-JGOY8xqq/lib/python3.10/site-packages/rest\_framework/viewsets.py”, line 125, in view  
return self.dispatch(request, \*args, \*\*kwargs)  
File “/Users/rogerlooser/.local/share/virtualenvs/storefront-JGOY8xqq/lib/python3.10/site-packages/rest\_framework/views.py”, line 509, in dispatch  
response = self.handle\_exception(exc)  
File “/Users/rogerlooser/.local/share/virtualenvs/storefront-JGOY8xqq/lib/python3.10/site-packages/rest\_framework/views.py”, line 469, in handle\_exception  
self.raise\_uncaught\_exception(exc)  
File “/Users/rogerlooser/.local/share/virtualenvs/storefront-JGOY8xqq/lib/python3.10/site-packages/rest\_framework/views.py”, line 480, in raise\_uncaught\_exception  
raise exc  
File “/Users/rogerlooser/.local/share/virtualenvs/storefront-JGOY8xqq/lib/python3.10/site-packages/rest\_framework/views.py”, line 506, in dispatch  
response = handler(request, \*args, \*\*kwargs)  
File “/Users/rogerlooser/Documents/Privat\_Roger/Schule/Python/storefront/store/views.py”, line 91, in me  
(customer, created) = Customer.objects.get\_or\_create(user\_id=request.user.id)  
File “/Users/rogerlooser/.local/share/virtualenvs/storefront-JGOY8xqq/lib/python3.10/site-packages/django/db/models/manager.py”, line 85, in manager\_method  
return getattr(self.get\_queryset(), name)(\*args, \*\*kwargs)  
File “/Users/rogerlooser/.local/share/virtualenvs/storefront-JGOY8xqq/lib/python3.10/site-packages/django/db/models/query.py”, line 935, in get\_or\_create  
return self.create(\*\*params), True  
File “/Users/rogerlooser/.local/share/virtualenvs/storefront-JGOY8xqq/lib/python3.10/site-packages/django/db/models/query.py”, line 671, in create  
obj.save(force\_insert=True, using=self.db)  
File “/Users/rogerlooser/.local/share/virtualenvs/storefront-JGOY8xqq/lib/python3.10/site-packages/django/db/models/base.py”, line 812, in save  
self.save\_base(  
File “/Users/rogerlooser/.local/share/virtualenvs/storefront-JGOY8xqq/lib/python3.10/site-packages/django/db/models/base.py”, line 863, in save\_base  
updated = self.\_save\_table(  
File “/Users/rogerlooser/.local/share/virtualenvs/storefront-JGOY8xqq/lib/python3.10/site-packages/django/db/models/base.py”, line 1006, in \_save\_table  
results = self.\_do\_insert(  
File “/Users/rogerlooser/.local/share/virtualenvs/storefront-JGOY8xqq/lib/python3.10/site-packages/django/db/models/base.py”, line 1047, in \_do\_insert  
return manager.\_insert(  
File “/Users/rogerlooser/.local/share/virtualenvs/storefront-JGOY8xqq/lib/python3.10/site-packages/django/db/models/manager.py”, line 85, in manager\_method  
return getattr(self.get\_queryset(), name)(\*args, \*\*kwargs)  
File “/Users/rogerlooser/.local/share/virtualenvs/storefront-JGOY8xqq/lib/python3.10/site-packages/django/db/models/query.py”, line 1790, in \_insert  
return query.get\_compiler(using=using).execute\_sql(returning\_fields)  
File “/Users/rogerlooser/.local/share/virtualenvs/storefront-JGOY8xqq/lib/python3.10/site-packages/django/db/models/sql/compiler.py”, line 1660, in execute\_sql  
cursor.execute(sql, params)  
File “/Users/rogerlooser/.local/share/virtualenvs/storefront-JGOY8xqq/src/django-debug-toolbar/debug\_toolbar/panels/sql/tracking.py”, line 230, in execute  
return self.\_record(self.cursor.execute, sql, params)  
File “/Users/rogerlooser/.local/share/virtualenvs/storefront-JGOY8xqq/src/django-debug-toolbar/debug\_toolbar/panels/sql/tracking.py”, line 154, in \_record  
return method(sql, params)  
File “/Users/rogerlooser/.local/share/virtualenvs/storefront-JGOY8xqq/lib/python3.10/site-packages/django/db/backends/utils.py”, line 103, in execute  
return super().execute(sql, params)  
File “/Users/rogerlooser/.local/share/virtualenvs/storefront-JGOY8xqq/lib/python3.10/site-packages/django/db/backends/utils.py”, line 67, in execute  
return self.\_execute\_with\_wrappers(  
File “/Users/rogerlooser/.local/share/virtualenvs/storefront-JGOY8xqq/lib/python3.10/site-packages/django/db/backends/utils.py”, line 80, in \_execute\_with\_wrappers  
return executor(sql, params, many, context)  
File “/Users/rogerlooser/.local/share/virtualenvs/storefront-JGOY8xqq/lib/python3.10/site-packages/django/db/backends/utils.py”, line 89, in \_execute  
return self.cursor.execute(sql, params)  
File “/Users/rogerlooser/.local/share/virtualenvs/storefront-JGOY8xqq/lib/python3.10/site-packages/django/db/backends/mysql/base.py”, line 80, in execute  
raise IntegrityError(\*tuple(e.args))  
django.db.utils.IntegrityError: (1048, “Column ‘user\_id’ cannot be null”)  
[26/Dec/2022 15:16:38] “GET /store/customers/me/ HTTP/1.1” 500 206743  
/Users/rogerlooser/Documents/Privat\_Roger/Schule/Python/storefront/store/views.py changed, reloading.  
Watching for file changes with StatReloader  
Performing system checks…

Maybe somebody of you guys find the issue

---

<div class="post-metadata">

### Author: ![nauman](https://avatars.discourse-cdn.com/v4/letter/n/779978/32.png) [@nauman](https://forum.codewithmosh.com/u/nauman)
#### Post date: [June 22, 2023, 10:12am UTC](https://forum.codewithmosh.com/t/django-part-2-10-getting-current-users-profile/17250/2 "2023-06-22T10:12:20Z")

</div>

Hi @Trycos, I was having the same issue after the lesson when sending requests to “/store/customers/me” route.

The reason is we have allowed non-authenticated requests to “/me” route and it expects the user to be authenticated.

In non-authenticated requests, `request.user` returns an AnonymousUser class object and `request.user.id` returns None. And, in “/me” action of CustomerViewSet at line:

`(customer, created) = Customer.objects.get_or_create(user_id=request.user.id)`

user\_id argument is given None value which raises the Exception because user\_id cannot be null when creating a customer.

To solve this we should not allow non-authenticated requests to “/me” action and here is how I did it:

```
def get_permissions(self):
    if self.request.method == "GET" and self.action != "me":
        return [AllowAny()]
    return [IsAuthenticated()]

```

I added another check `self.action != "me"` in get\_permissions function of CustomerViewSet.

---

<div class="post-metadata">

### Author: ![Jhay](https://avatars.discourse-cdn.com/v4/letter/j/7cd45c/32.png) [@Jhay](https://forum.codewithmosh.com/u/Jhay)
#### Post date: [September 25, 2023, 2:54pm UTC](https://forum.codewithmosh.com/t/django-part-2-10-getting-current-users-profile/17250/3 "2023-09-25T14:54:07Z")

</div>

I have presently facing @Trycos issues and there seems to be no way out. I tired @nauman implementations but the AllowAny() and IsAuthenticated() call got highlighted. Please guys, help is really needed here.  
Thanks.

---

<div class="post-metadata">

### Author: ![AmirScheldon](https://avatars.discourse-cdn.com/v4/letter/a/ecc23a/32.png) [@AmirScheldon](https://forum.codewithmosh.com/u/AmirScheldon)
#### Post date: [February 6, 2024, 6:49pm UTC](https://forum.codewithmosh.com/t/django-part-2-10-getting-current-users-profile/17250/4 "2024-02-06T18:49:44Z")

</div>

hey, I have same issue and I used @nauman solution too but not answered. Anybody has reached to a solution?
