Error trying to create Post view with function based view

here is the error

File "C:\Users\Joseph Mkonda\.virtualenvs\TalklineAPI-trkn4jEg\lib\site-packages\rest_framework\", line 506, in dispatch
    response = handler(request, *args, **kwargs)
  File "C:\Users\Joseph Mkonda\.virtualenvs\TalklineAPI-trkn4jEg\lib\site-packages\rest_framework\", line 50, in handler
    return func(*args, **kwargs)
  File "C:\Users\Joseph Mkonda\Desktop\GreenField-1.0\talklineapi\posts\", line 28, in post_create
  File "C:\Users\Joseph Mkonda\.virtualenvs\TalklineAPI-trkn4jEg\lib\site-packages\rest_framework\", line 212, in save
    self.instance = self.create(validated_data)
  File "C:\Users\Joseph Mkonda\.virtualenvs\TalklineAPI-trkn4jEg\lib\site-packages\rest_framework\", line 962, in create
    instance = ModelClass._default_manager.create(**validated_data)
  File "C:\Users\Joseph Mkonda\.virtualenvs\TalklineAPI-trkn4jEg\lib\site-packages\django\db\models\", line 87, in manager_method     
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "C:\Users\Joseph Mkonda\.virtualenvs\TalklineAPI-trkn4jEg\lib\site-packages\django\db\models\", line 675, in create
    obj = self.model(**kwargs)
  File "C:\Users\Joseph Mkonda\.virtualenvs\TalklineAPI-trkn4jEg\lib\site-packages\django\db\models\", line 543, in __init__
    _setattr(self,, rel_obj)
  File "C:\Users\Joseph Mkonda\.virtualenvs\TalklineAPI-trkn4jEg\lib\site-packages\django\db\models\fields\", line 283, in __set__
    raise ValueError(
ValueError: Cannot assign "<django.contrib.auth.models.AnonymousUser object at 0x0000025B12A2F910>": "Post.user" must be a "User" instance.     
[11/Jan/2024 19:50:46] "POST /api/create/ HTTP/1.1" 500 136095

here the view

def post_create(request):
    serializer = PostSerializer(
    if serializer.is_valid():
        return Response(, status=status.HTTP_201_CREATED)
    return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

and the serializer

class PostSerializer(ModelSerializer):
    user = userDetailsSerializer(read_only=True)

    class Meta:
        model = Post
        fields = [

the model

class PostManager(models.Manager):
    def active(self, *args, **kwargs):
        return super(PostManager, self).filter(draft=False).filter(

def upload_location(instance, filename):
    PostModel = instance.__class__
    obj_exist = PostModel.objects.order_by("id").last()
    new_id = 1

    if obj_exist:
        new_id = + 1

    return "%s/%s" % (new_id, filename)

class Post(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
    content = models.TextField()
    picture = models.ImageField(upload_to=upload_location, null=True, blank=True)
    published = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)

    objects = PostManager()

    def __unicode__(self):
        return self.content

    def __str__(self):
        return self.content

    class Meta:
        ordering = ['-published', '-updated']

    def get_absolute_url(self):
        return reverse("posts:detail", kwargs={"slug": self.slug})

    def get_api_url(self):
        return reverse("posts-api:detail", kwargs={"slug": self.slug})

    def get_content_type(self):
        instance = self
        content_type = ContentType.objects.get_for_model(instance.__class__)
        return content_type

def create_slug(instance, new_slug=None):
    slug = slugify(instance.content)

    if new_slug is not None:
        slug = new_slug

    qs = Post.objects.filter(slug=slug).order_by('-id')
    exists = qs.exists()

    if exists:
        new_slug = "%s-%s" % (slug, qs.first().id)

    return slug

Side note: When posting code here, enclose the code between lines of three backtick - ` characters. This means you’ll have a line of ```, then the code, then another line of ```. This forces the forum software to keep your code properly formatted. (I’ve taken the liberty of modifying your original post for this.)

If you look at the traceback, the error is being caused here:

The error it’s giving you is this:

This is telling you that whatever is submitting this data is not authenticated and does not have an active session.

thank you for your humbleness for the task you have done for me. Am new this Django drf so help me how can i test the end point with authorized in postman have already tested the end point with JWT token but keeps me giving the error like the user is not authenticated after removing the Isauthenticated permissions decorator it gives me that error help me to test both with token and without token

You would need to include the JWT token in every request.

If you’re having problems with a JWT token, that’s a different issue that would need to be addressed.

already include jwt in every request but still getting the error

Is the token working for you in other views?