我得到了queryset,如何得到相对对象?

时间:2023-01-27 16:00:49

I get the relative objects from queryset, but it have a further relative query, how to get those objects?

我从queryset中获取相对对象,但是它有一个进一步的相对查询,如何获取这些对象?

class PropertyTaxItem(models.Model):
    property = models.ForeignKey(Property)

class Property(models.Model):
    citizens = models.ManyToManyField(Citizen, null=True, through = 'Ownership',help_text="a property could belong to multiple citizens")

class Citizen(models.Model):
   first_name = models.CharField(max_length = 50, help_text = 'First name')
   last_name = models.CharField(max_length = 50, help_text = 'Last name')

my getting the citzien part:

我得到的citzien部分:

items = PropertyTaxItem.objects.filter(i_status='active').select_related('property_id').prefetch_related('property_id.citizens')
for i in items:
    pp.pprint(i.citizens.__dict__)

the output is:

的输出是:

{'_db': None,
'_fk_val': 10092,
'_inherited': False,
'core_filters': {'property__pk': 10092},
'creation_counter': 69,
'instance': <Property: 306 GASHIHA, KAGINA, Kicukiro,Kicukiro>,
'model': <class 'citizen.models.Citizen'>,
'prefetch_cache_name': 'citizens',
'query_field_name': 'property',
'reverse': False,
'source_field_name': 'asset_property',
'symmetrical': False,
'target_field_name': 'owner_citizen',
'through': <class 'property.models.Ownership'>}

but I want get the items of citizens like:

但我想获得市民的物品,比如:

{'id': 18980,
 'first_name': 'Jack',
 'last_name' : 'blablabla',
 ....
 }

how to do it?

如何去做?

1 个解决方案

#1


2  

You have a few issues. You are querying PropertyTaxItem which foreign keys to Property, which then has multiple Citizens. This would be a correct query to select the related 'propertys and prefetch theproperty.citizens`

你有一些问题。您正在查询PropertyTaxItem哪个外键指向属性,然后该属性有多个公民。这将是一个正确的查询来选择相关的'propertys并预取property.citizens '

items = PropertyTaxItem.objects.filter(i_status='active').select_related('property').prefetch_related('property__citizens')

You can access the citizens like this:

你可以像这样接近市民:

for i in items:
    citizens = i.property.citizens.all() # now you have a queryset of citizens. 
    # To get a list of dictionaries, you can call `.values()` on the queryset.
    pp.pprint(citizens.values())

Another possible problem you will have is that citizens uses a custom through model. There are some limitations on ManyToManyField query sets when using custom though models.

您可能遇到的另一个问题是,市民通过模型使用自定义。在使用自定义通过模型时,许多tomanyfield查询集都有一些限制。

#1


2  

You have a few issues. You are querying PropertyTaxItem which foreign keys to Property, which then has multiple Citizens. This would be a correct query to select the related 'propertys and prefetch theproperty.citizens`

你有一些问题。您正在查询PropertyTaxItem哪个外键指向属性,然后该属性有多个公民。这将是一个正确的查询来选择相关的'propertys并预取property.citizens '

items = PropertyTaxItem.objects.filter(i_status='active').select_related('property').prefetch_related('property__citizens')

You can access the citizens like this:

你可以像这样接近市民:

for i in items:
    citizens = i.property.citizens.all() # now you have a queryset of citizens. 
    # To get a list of dictionaries, you can call `.values()` on the queryset.
    pp.pprint(citizens.values())

Another possible problem you will have is that citizens uses a custom through model. There are some limitations on ManyToManyField query sets when using custom though models.

您可能遇到的另一个问题是,市民通过模型使用自定义。在使用自定义通过模型时,许多tomanyfield查询集都有一些限制。