Search code examples
djangodjango-rest-frameworkaxiosdjango-authenticationsetter

How to set userkey_id with @setter and token in django?


I have created a client frontend and have tested the url using cUrl and it works, sending the auth token via axios headers allows me in to call the api. The problem is that I get a NOT NULL constraint failed: post_post.userkey_id error and have narrowed it down that the @user.setter is not getting the CustomUser from the Auth Token. How can I correctly use the @user.setter to set the user that has the corresponding auth token/created the post from the client frontend.

Views.py

class CreatePost(generics.CreateAPIView):
    serializer_class = PostSerializer
    permission_classes = [permissions.IsAuthenticated]

    def perform_create(self,serializer):
        serializer.save(user = self.request.user)

Post model.py

from django.db import models
from accounts.models import CustomUser

class Post(models.Model):
    #foriegn keys
    userkey = models.ForeignKey(CustomUser, on_delete=models.CASCADE) 

    #properties
    title = models.CharField(max_length=100,blank=True)
    image= models.FileField(upload_to='files/', null=True, verbose_name="",blank=True) #image field
    price = models.IntegerField(default=0,blank=True)
    description = models.TextField(blank=True)
    likes = models.IntegerField(default=0)
    tags = models.CharField(max_length=30,blank=True,default="tag1,tag2,...") #sqlite doesnt support arrays
    date_modified = models.DateTimeField(auto_now=True,blank=True)
    created = models.DateTimeField(auto_now_add=True,blank=True)
    sold = models.BooleanField(default=False,blank=True)

    @property
    def user(self):
        return self.userkey.username

    @user.setter
    def user(self):
        return models.ForeignKey(CustomUser,on_delete=models.CASCADE)

    @property
    def datejoined(self):
        return self.userkey.date_joined

    def __str__(self):
        return self.title

The userkey is to get data from the user thus I have a @property def user function to collect data from another object. The userkey and @property def user work fine from the admin panel. The @user.setter is used in order to allow the user to be changed from the views.py otherwise I get a "cannot change attribute 'user'" error. Thus I know that the problem is specifically from the @user.setter, I just don't know what I am doing wrong, everything seems fine. Post creation only seems to work from the Admin panel.


Solution

  • Fixed, the error was in the views.py

    class CreatePost(generics.CreateAPIView):
        serializer_class = PostSerializer
        permission_classes = [permissions.IsAuthenticated]
        
        def perform_create(self,serializer):
            serializer.save(userkey = self.request.user)
    

    Should be changing the 'userkey' not the 'user'. Also removed the user.setter. I was trying to change the user but it should have been the userkey as that is the field and user is a property.