Facing troubles with getting values from foreign key models. I have one model, where included all foreign key relations.
class UserAccount(models.Model):
name= models.CharField(max_length=100)
surname = models.CharField(max_length=100)
category= models.ManyToManyField(Category)
account = models.ForeignKey(Account)
country = models.ForeignKey(Country)
permissions = models.ForeignKey(Permissions)
class Country(models.Model):
iso_code = models.CharField(max_length=6)
zip_code = models.CharField(max_length=10)
I'm using this to get all fields related to model UserAccount:
user_account_data = UserAccount.objects.all()
name = user_account_data.values_list('name', flat=True)))
surname = user_account_data.values_list('surname', flat=True)))
But when trying this, its giving me: 'QuerySet' object has no attribute 'country'
countries = user_account_data.country.values('iso_code')
CodePudding user response:
try this
countries = user_account_data.values('country__iso_code')
https://docs.djangoproject.com/en/4.0/ref/models/querysets/#django.db.models.query.QuerySet.values