Anget
Anget

Reputation: 61

Trying to create a view to set booleanfield to true but not updating

All of the users have a booleanfield first_visit that is set to true.

I created an alert with some information on the homepage and with a button at the end, when they click it, it will set the booleanfield to false and they will no longer see the "alert", its like a guide...

I extended the user model from django so this is what I did :

models.py

class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
first_visit = models.BooleanField(default=True)

My views.py

def verification(request, user_id):
    user = User.objects.get(pk=user_id)
    user.first_visit = False
    user.save()

My path

path('first_visit/<user_id>', views.verification, name="firstvisit"),

And the link in my template

{% url 'firstvisit' user.id %}

But when i tried, it does not affect the model and it still set to true

Could you help me ?

Upvotes: 0

Views: 28

Answers (1)

PythonNewbie
PythonNewbie

Reputation: 828

You're trying to edit firs_visit field in built-in User model, but its defined in extended Profile model, I think your answer is:

My views.py

def verification(request, user_id):
    user = User.objects.get(pk=user_id)
    user.profile.first_visit = False
    user.profile.save()


Also, if you implemented authenticated system like in Django documentation, and edit your Profile model a little bit, you could do it more easily:

My views.py

def verification(request, user_id):
    request.user.profile.first_visit = False
    request.user.profile.save()

models.py

class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE, related_name="profile")
    first_visit = models.BooleanField(default=True)

Upvotes: 1

Related Questions