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')
.values/.values_list: django-antipatterns.com/antipattern/over-use-of-values.htmlfor item in user_account_data, and then you can accessitem.name,item.surnameanditem.country.iso_code. You can boost efficiency by usingUserAccount.objects.select_related('country'), but this will not erode the model layer.