1

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')
3
  • Please don't use .values/.values_list: django-antipatterns.com/antipattern/over-use-of-values.html Commented Apr 5, 2022 at 8:44
  • ok, anyway if I use only or filter its giving the same, because there is some issue for getting country model, that is the main trouble for me Commented Apr 5, 2022 at 8:48
  • use for item in user_account_data, and then you can access item.name, item.surname and item.country.iso_code. You can boost efficiency by using UserAccount.objects.select_related('country'), but this will not erode the model layer. Commented Apr 5, 2022 at 8:49

2 Answers 2

4

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

Sign up to request clarification or add additional context in comments.

Comments

1

Please do not make use of .values(…) [Django-doc] or .values_list(…) [Django-doc]: first of all you will here make one query for each value so if you evaluate name, surname, and countries, then you make three queries. Furthermore it is not even said that these queries will produce results in the same order, so matching the items will be a second problem, and finally this will produce dictionaries/lists of values: these thus "erode" the model layer, and introduce a primitive obsession antipattern [refactoring.guru].

Usually one works with model objects, you can thus fetch all the data for the UserAccount with the corresponding Country data with:

for item in UserAccount.objects.select_related('country'):
    print(item.name)
    print(item.surname)
    print(item.country.iso_code)

You can thus use these in a template, in a serializer, form, etc. It guarantees that the .name, .surname and .country.iso_code are of the same item. item.country is a Country object: so you can pass this to functions that work with a Country object (for example serialize, update, etc.). It is thus more convenient to work with model objects, than with dictionaries/lists/… that contain data of these objects.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.