How to use choices in model field

51 views
Skip to first unread message

Aaryan R Soni

unread,
Nov 2, 2022, 10:53:17 AM11/2/22
to Django users
Hey guys, i am facing some issues using choices in models.CharField. I am creating a model which has username and status, where status is my choice field. it has 2 values, 'Unverified' and 'Verified'.

class user_stutus(models.Model):
    status_choices=[('Unverified','Unverified'),('Verified','Verified')]
    uname=models.OneToOneField(User,on_delete=models.CASCADE)
    status=models.CharField(max_length=11,choices=status_choices,default='Unverified')

def verifyCode(request):
    global errors,session
    if request.method == 'POST':
        code = request.POST['code']
        user=User.objects.get(email=session['email'])
        print('user',user)
        print('request',session['email'])
        if session['code'] != code:
            errors['code'] = 'Verification code did not match!'
            return redirect('/user/user-verify/')
        verification = user_stutus.objects.get(username=user)
        verification.status = 'Verified'    #here i am changing its value and facing the issue.
        return redirect('/user/user-login/')

can someone give me any solution for this?
Thank you!

 

DISCLAIMER

Any views or opinions presented in this email are solely those of the author and do not necessarily represent those of the Ganpat University.

 


 

DISCLAIMER

Any views or opinions presented in this email are solely those of the author and do not necessarily represent those of the Ganpat University.

 

kateregga julius

unread,
Nov 2, 2022, 12:21:15 PM11/2/22
to django...@googlegroups.com
Don't use square blackest [ ]

status_choices=(('Unverified','Unverified'),('Verified','Verified'))

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/6ee9dae8-2879-4c3d-a921-238b36fcb975n%40googlegroups.com.

Aaryan R Soni

unread,
Nov 2, 2022, 12:24:02 PM11/2/22
to django...@googlegroups.com
Okay, but still the problem is that how do I set the value of status field, as I have written in the code, verification.status = 'Verified' it is not giving any error but it is not working either

Julio Cojom

unread,
Nov 2, 2022, 12:27:54 PM11/2/22
to django...@googlegroups.com
When you change the status to Verified, you are only changing the object instance in memory, to save it to the database, you need to call the method save.

verification.save() at the end should make it work 

kateregga julius

unread,
Nov 2, 2022, 12:31:21 PM11/2/22
to django...@googlegroups.com
Apply a CRUD for updating the status
Wait I will send you the updated code in 30 minutes 

Lloyd Leckenby

unread,
Nov 2, 2022, 1:05:04 PM11/2/22
to 'starryrbs' via Django users

kateregga julius

unread,
Nov 2, 2022, 1:20:32 PM11/2/22
to django...@googlegroups.com
Follow exactly what he has told you 

subin

unread,
Nov 3, 2022, 3:25:51 PM11/3/22
to django...@googlegroups.com

subin

unread,
Nov 3, 2022, 3:27:24 PM11/3/22
to django...@googlegroups.com
Thanks for your first XYZ Company purchase! We’re excited for you to try out our product. Here’s how you can set it up

subin

unread,
Nov 3, 2022, 3:29:31 PM11/3/22
to django...@googlegroups.com
I will continue learning more about via.gov.

subin

unread,
Nov 3, 2022, 3:30:54 PM11/3/22
to django...@googlegroups.com

subin

unread,
Nov 3, 2022, 3:40:02 PM11/3/22
to django...@googlegroups.com

subin

unread,
Nov 3, 2022, 3:43:23 PM11/3/22
to django...@googlegroups.com
Hello there

--

Aaryan R Soni

unread,
Nov 4, 2022, 2:49:57 AM11/4/22
to django...@googlegroups.com
Thanks, that worked, Thanks for your help

Reply all
Reply to author
Forward
0 new messages