soubhagya
soubhagya

Reputation: 806

how to get all fields of related model in django instead of only id

models.py

from django.db import models

class SeekerRegister(models.Model):
    seeker_name       = models.CharField(max_length=32)
    seeker_email      = models.CharField(max_length=32)

class Social(models.Model):
    social_links      = models.CharField(max_length=256)
    user              = models.ForeignKey(access_models.SeekerRegister,on_delete=models.CASCADE,related_name='social',null=True,blank=True)

my query:

    >>>obj=list(SeekerRegister.objects.values('social'))
    >>>[{'social': 1}, {'social': 2}, {'social': 3}]

expecting:

    [{'social_links': 'facebook.com','user':1,'seeker_name':'a'}, {'social_links': 'twitter.com','user':2,'seeker_name':'b'}, {'social_links': 'linkedin.com','user':3,'seeker_name':'c'}]

when i am writing the above query i am getting only id of social model. how can i get all fields both social_links and user instead.

please have a look into my code.

Upvotes: 0

Views: 91

Answers (1)

ruddra
ruddra

Reputation: 51988

You can try like this:

SeekerRegister.objects.values('social__social_link', 'id', 'seeker_name') 

where social__social_link is the social_link and id is user

Pleas check this SO Answer for more details.

Upvotes: 2

Related Questions