I can't add a value to this user model field

Question:

I’m trying to do a system where an user gains points if he asks a question but the points field isn’t increasing when a user does that.

my model:

class Post(models.Model):
    author = models.ForeignKey(User, on_delete=models.PROTECT, related_name='post')
    category = models.ForeignKey(Category, on_delete=models.PROTECT)
    type = models.CharField(max_length=30, choices=TYPE, default='Question')

    title = models.CharField(max_length=100, unique=True)
    content = models.TextField()

    views = models.IntegerField(default=0)
    votes = models.ManyToManyField(User, blank=True, related_name='vote')
    featured = models.BooleanField(default=False)

    date_posted = models.DateTimeField(default=timezone.now)

my view:

class PostCreateView(LoginRequiredMixin, CreateView):
    model = Post
    success_url = '/'
    fields = ['title', 'content', 'category']

    def form_valid(self, form):
        form.instance.author = self.request.user
        form.instance.author.points + 15
        return super().form_valid(form)

When I go to the current user in the admin page the value doesn’t change.

Asked By: nameless

||

Answers:

You should add more clarity to your code, but as I can assume and as Thierno said you are not accessing the object and not saving it afterwards.

What you need to do is once you make your post request, –and since you need access to the user–, save your post and then do something like:

post_user = self.request.user 
post_user.points +=15
post_user.save()
Answered By: haduki