简体   繁体   中英

How to get only one field by comparing other field of same model in view file of Django

I want to run this query:

"SELECT pname FROM PaientSignup WHERE email=p_email"

I used filter method that gives me all other data that match with email given.

p= PatientSignup.objects.filter(email=p_email)

From that output I could not able to fetch on name of patient.

  • models.py

     class PatientSignup(models.Model): pid = models.AutoField(verbose_name='Patient Id', primary_key=True, auto_created=True) pname = models.CharField(verbose_name='Enter Name', max_length=50, default=NameError) email = models.CharField(verbose_name='Enter Email', max_length=100,unique=True) age = models.PositiveIntegerField(verbose_name='Enter age',default=5, null=True) password = models.CharField(verbose_name='Enter Password',max_length=12) 
  • views.py

     def pFeedback(request): #feedback = textarea input p_email = request.session['pusername'] #here, I want only patient name->pname to store in database saveFeedback = patientFeedback() saveFeedback.feedback = feedback saveFeedback.patientName = patient saveFeedback.save() 

Try PatientSignup.objects.filter(email=p_email).values_list('pname', flat=True) . It will returns list of names matching with email.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM