ManyToManyField增加的额外关系列,如何查询query

https://docs.djangoproject.com/en/2.0/topics/db/models/#extra-fields-on-many-to-many-relationships

Unlike normal many-to-many fields, you can’t use add(), create(),or set() to create relationships:

>>> # The following statements will not work
>>> beatles.members.add(john)
>>> beatles.members.create(name="George Harrison")
>>> beatles.members.set([john, paul, ringo, george])

Why? You can’t just create a relationship between a Person and a Group- you need to specify all the detail for the relationship required by theMembership model. The simple add, create and assignment callsdon’t provide a way to specify this extra detail. As a result, they aredisabled for many-to-many relationships that use an intermediate model.The only way to create this type of relationship is to create instances of theintermediate model.

The remove() method isdisabled for similar reasons. For example, if the custom through table definedby the intermediate model does not enforce uniqueness on the(model1, model2) pair, a remove() call would not provide enoughinformation as to which intermediate model instance should be deleted:

>>> Membership.objects.create(person=ringo, group=beatles,
...     date_joined=date(1968, 9, 4),
...     invite_reason="You've been gone for a month and we miss you.")
>>> beatles.members.all()
<QuerySet [<Person: Ringo Starr>, <Person: Paul McCartney>, <Person: Ringo Starr>]>
>>> # This will not work because it cannot tell which membership to remove
>>> beatles.members.remove(ringo)

However, the clear()method can be used to remove all many-to-many relationships for an instance:

>>> # Beatles have broken up
>>> beatles.members.clear()
>>> # Note that this deletes the intermediate model instances
>>> Membership.objects.all()
<QuerySet []>

Once you have established the many-to-many relationships by creating instancesof your intermediate model, you can issue queries. Just as with normalmany-to-many relationships, you can query using the attributes of themany-to-many-related model:

# Find all the groups with a member whose name starts with 'Paul'
>>> Group.objects.filter(members__name__startswith='Paul')
<QuerySet [<Group: The Beatles>]>

As you are using an intermediate model, you can also query on its attributes:

# Find all the members of the Beatles that joined after 1 Jan 1961
>>> Person.objects.filter(
...     group__name='The Beatles',
...     membership__date_joined__gt=date(1961,1,1))
<QuerySet [<Person: Ringo Starr]>

If you need to access a membership’s information you may do so by directlyquerying the Membership model:

>>> ringos_membership = Membership.objects.get(group=beatles, person=ringo)
>>> ringos_membership.date_joined
datetime.date(1962, 8, 16)
>>> ringos_membership.invite_reason
'Needed a new drummer.'

Another way to access the same information is by querying themany-to-many reverse relationship from aPerson object:

>>> ringos_membership = ringo.membership_set.get(group=beatles)
>>> ringos_membership.date_joined
datetime.date(1962, 8, 16)
>>> ringos_membership.invite_reason
'Needed a new drummer.'

from first_app.models import Record
record_set = Record.objects.all()
record = record_set.first()
record.complaintri_set.first()
<ComplaintRI: ComplaintRI object (136)>
record.complaintri_set.first().where
<django.db.models.fields.related_descriptors.create_forward_many_to_many_manager.<locals>.ManyRelatedManager object at 0x00000176F0BE98D0>
record.complaintri_set.first().where.all()
<QuerySet [<Poi: 双辽农场>, <Poi: 双辽农场>, <Poi: 国营双辽农场>, <Poi: 双辽农场中学>, <Poi: 双辽农场中学(东南门)>, <Poi: 四平市双辽农场大理石场>, <Poi: 双辽农场三分场>, <Poi: 双辽农场中心小学>, <Poi: 国营双辽农场(东南门)>, <Poi: 双辽农场林业站>, <Poi: 双辽农场农业队>, <Poi: 双辽农场马队>, <Poi: 辽河星区双辽农场南桥驾校>, <Poi: 双辽农场畜牧兽医站>, <Poi: 双山镇双山鸭场>, <Poi: 双辽华源农机农场分公司>, <Poi: 辽河星区双辽农场南桥驾校(西北门)>, <Poi: 中共双辽农场三分场总支委员会>, <Poi: 双辽农场农机管理服务站>, <Poi: 镇芝楼农场职工医院>]>
record.complaintri_set.first().where.filter(order=1)

最后一句报错:

Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "C:\Users\Administrator\PycharmProjects\first_django\venv\lib\site-packages\django\db\models\manager.py", line 82, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "C:\Users\Administrator\PycharmProjects\first_django\venv\lib\site-packages\django\db\models\query.py", line 836, in filter
    return self._filter_or_exclude(False, *args, **kwargs)
  File "C:\Users\Administrator\PycharmProjects\first_django\venv\lib\site-packages\django\db\models\query.py", line 854, in _filter_or_exclude
    clone.query.add_q(Q(*args, **kwargs))
  File "C:\Users\Administrator\PycharmProjects\first_django\venv\lib\site-packages\django\db\models\sql\query.py", line 1253, in add_q
    clause, _ = self._add_q(q_object, self.used_aliases)
  File "C:\Users\Administrator\PycharmProjects\first_django\venv\lib\site-packages\django\db\models\sql\query.py", line 1277, in _add_q
    split_subq=split_subq,
  File "C:\Users\Administrator\PycharmProjects\first_django\venv\lib\site-packages\django\db\models\sql\query.py", line 1153, in build_filter
    lookups, parts, reffed_expression = self.solve_lookup_type(arg)
  File "C:\Users\Administrator\PycharmProjects\first_django\venv\lib\site-packages\django\db\models\sql\query.py", line 1015, in solve_lookup_type
    _, field, _, lookup_parts = self.names_to_path(lookup_splitted, self.get_meta())
  File "C:\Users\Administrator\PycharmProjects\first_django\venv\lib\site-packages\django\db\models\sql\query.py", line 1379, in names_to_path
    "Choices are: %s" % (name, ", ".join(available)))
django.core.exceptions.FieldError: Cannot resolve keyword 'order' into field. Choices are: address, address_id, adname, adname_id, amapid, cityname, cityname_id, complaintrelatedamappois, complaintri, id, location, location_id, name, name_id, pname, pname_id, poicategory, poicategory_id



以上是测试代码,下面也是:

record.complaintri_set.first().complaintrelatedamappois_set
<django.db.models.fields.related_descriptors.create_reverse_many_to_one_manager.<locals>.RelatedManager object at 0x00000176F0C086A0>
record.complaintri_set.first().complaintrelatedamappois_set.all()
<QuerySet [<ComplaintRelatedAmapPois: ComplaintRelatedAmapPois object (1220)>, <ComplaintRelatedAmapPois: ComplaintRelatedAmapPois object (1221)>, <ComplaintRelatedAmapPois: ComplaintRelatedAmapPois object (1222)>, <ComplaintRelatedAmapPois: ComplaintRelatedAmapPois object (1223)>, <ComplaintRelatedAmapPois: ComplaintRelatedAmapPois object (1224)>, <ComplaintRelatedAmapPois: ComplaintRelatedAmapPois object (1225)>, <ComplaintRelatedAmapPois: ComplaintRelatedAmapPois object (1226)>, <ComplaintRelatedAmapPois: ComplaintRelatedAmapPois object (1227)>, <ComplaintRelatedAmapPois: ComplaintRelatedAmapPois object (1228)>, <ComplaintRelatedAmapPois: ComplaintRelatedAmapPois object (1229)>, <ComplaintRelatedAmapPois: ComplaintRelatedAmapPois object (1230)>, <ComplaintRelatedAmapPois: ComplaintRelatedAmapPois object (1231)>, <ComplaintRelatedAmapPois: ComplaintRelatedAmapPois object (1232)>, <ComplaintRelatedAmapPois: ComplaintRelatedAmapPois object (1233)>, <ComplaintRelatedAmapPois: ComplaintRelatedAmapPois object (1234)>, <ComplaintRelatedAmapPois: ComplaintRelatedAmapPois object (1235)>, <ComplaintRelatedAmapPois: ComplaintRelatedAmapPois object (1236)>, <ComplaintRelatedAmapPois: ComplaintRelatedAmapPois object (1237)>, <ComplaintRelatedAmapPois: ComplaintRelatedAmapPois object (1238)>, <ComplaintRelatedAmapPois: ComplaintRelatedAmapPois object (1239)>]>
record.complaintri_set.first().complaintrelatedamappois_set.filter(order=1)
<QuerySet [<ComplaintRelatedAmapPois: ComplaintRelatedAmapPois object (1220)>]>
record.complaintri_set.first().complaintrelatedamappois_set.filter(order=1).first().poi
<Poi: 双辽农场>
record.complaintri_set.first().complaintrelatedamappois_set.filter(order=1).first().poi.location
<LngLatGcj: 123.991020,43.697205>
record.complaintri_set.first().complaintrelatedamappois_set.filter(order=1).first().poi.location.latgcj
Decimal('43.697205')





猜你喜欢

转载自blog.csdn.net/qq_27361945/article/details/80051750