从 Django 中的查询集中获取值
Getting values from queryset in Django
从外键模型获取值时遇到问题。
我有一个模型,其中包括所有外键关系。
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)
我正在使用它来获取与模型 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)))
但是在尝试这个时,它给我:'QuerySet' object has no attribute 'country'
countries = user_account_data.country.values('iso_code')
试试这个
countries = user_account_data.values('country__iso_code')
https://docs.djangoproject.com/en/4.0/ref/models/querysets/#django.db.models.query.QuerySet.values
请不要使用.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]。
通常使用模型对象,因此您可以使用相应的 Country
数据获取 UserAccount
的所有数据:
for item in UserAccount.objects.select_related('country'):
print(item.name)
print(item.surname)
print(item.country.iso_code)
因此,您可以在模板、序列化程序、表单等中使用它们。它保证 .name
、.surname
和 .country.iso_code
属于 相同项。 item.country
是一个 Country
对象:因此您可以将其传递给使用 Country
对象的函数(例如序列化、更新等)。因此,使用模型对象比使用包含这些对象数据的 dictionaries/lists/... 更方便。
从外键模型获取值时遇到问题。 我有一个模型,其中包括所有外键关系。
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)
我正在使用它来获取与模型 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)))
但是在尝试这个时,它给我:'QuerySet' object has no attribute 'country'
countries = user_account_data.country.values('iso_code')
试试这个
countries = user_account_data.values('country__iso_code')
https://docs.djangoproject.com/en/4.0/ref/models/querysets/#django.db.models.query.QuerySet.values
请不要使用.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]。
通常使用模型对象,因此您可以使用相应的 Country
数据获取 UserAccount
的所有数据:
for item in UserAccount.objects.select_related('country'):
print(item.name)
print(item.surname)
print(item.country.iso_code)
因此,您可以在模板、序列化程序、表单等中使用它们。它保证 .name
、.surname
和 .country.iso_code
属于 相同项。 item.country
是一个 Country
对象:因此您可以将其传递给使用 Country
对象的函数(例如序列化、更新等)。因此,使用模型对象比使用包含这些对象数据的 dictionaries/lists/... 更方便。