fields.py 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  1. from collections import defaultdict
  2. from django.contrib.contenttypes.models import ContentType
  3. from django.core import checks
  4. from django.core.exceptions import FieldDoesNotExist, ObjectDoesNotExist
  5. from django.db import DEFAULT_DB_ALIAS, models, router, transaction
  6. from django.db.models import DO_NOTHING
  7. from django.db.models.base import ModelBase, make_foreign_order_accessors
  8. from django.db.models.fields.related import (
  9. ForeignObject, ForeignObjectRel, ReverseManyToOneDescriptor,
  10. lazy_related_operation,
  11. )
  12. from django.db.models.query_utils import PathInfo
  13. from django.utils.functional import cached_property
  14. class GenericForeignKey:
  15. """
  16. Provide a generic many-to-one relation through the ``content_type`` and
  17. ``object_id`` fields.
  18. This class also doubles as an accessor to the related object (similar to
  19. ForwardManyToOneDescriptor) by adding itself as a model attribute.
  20. """
  21. # Field flags
  22. auto_created = False
  23. concrete = False
  24. editable = False
  25. hidden = False
  26. is_relation = True
  27. many_to_many = False
  28. many_to_one = True
  29. one_to_many = False
  30. one_to_one = False
  31. related_model = None
  32. remote_field = None
  33. def __init__(self, ct_field='content_type', fk_field='object_id', for_concrete_model=True):
  34. self.ct_field = ct_field
  35. self.fk_field = fk_field
  36. self.for_concrete_model = for_concrete_model
  37. self.editable = False
  38. self.rel = None
  39. self.column = None
  40. def contribute_to_class(self, cls, name, **kwargs):
  41. self.name = name
  42. self.model = cls
  43. self.cache_attr = "_%s_cache" % name
  44. cls._meta.add_field(self, private=True)
  45. setattr(cls, name, self)
  46. def get_filter_kwargs_for_object(self, obj):
  47. """See corresponding method on Field"""
  48. return {
  49. self.fk_field: getattr(obj, self.fk_field),
  50. self.ct_field: getattr(obj, self.ct_field),
  51. }
  52. def get_forward_related_filter(self, obj):
  53. """See corresponding method on RelatedField"""
  54. return {
  55. self.fk_field: obj.pk,
  56. self.ct_field: ContentType.objects.get_for_model(obj).pk,
  57. }
  58. def __str__(self):
  59. model = self.model
  60. app = model._meta.app_label
  61. return '%s.%s.%s' % (app, model._meta.object_name, self.name)
  62. def check(self, **kwargs):
  63. errors = []
  64. errors.extend(self._check_field_name())
  65. errors.extend(self._check_object_id_field())
  66. errors.extend(self._check_content_type_field())
  67. return errors
  68. def _check_field_name(self):
  69. if self.name.endswith("_"):
  70. return [
  71. checks.Error(
  72. 'Field names must not end with an underscore.',
  73. obj=self,
  74. id='fields.E001',
  75. )
  76. ]
  77. else:
  78. return []
  79. def _check_object_id_field(self):
  80. try:
  81. self.model._meta.get_field(self.fk_field)
  82. except FieldDoesNotExist:
  83. return [
  84. checks.Error(
  85. "The GenericForeignKey object ID references the "
  86. "nonexistent field '%s'." % self.fk_field,
  87. obj=self,
  88. id='contenttypes.E001',
  89. )
  90. ]
  91. else:
  92. return []
  93. def _check_content_type_field(self):
  94. """
  95. Check if field named `field_name` in model `model` exists and is a
  96. valid content_type field (is a ForeignKey to ContentType).
  97. """
  98. try:
  99. field = self.model._meta.get_field(self.ct_field)
  100. except FieldDoesNotExist:
  101. return [
  102. checks.Error(
  103. "The GenericForeignKey content type references the "
  104. "nonexistent field '%s.%s'." % (
  105. self.model._meta.object_name, self.ct_field
  106. ),
  107. obj=self,
  108. id='contenttypes.E002',
  109. )
  110. ]
  111. else:
  112. if not isinstance(field, models.ForeignKey):
  113. return [
  114. checks.Error(
  115. "'%s.%s' is not a ForeignKey." % (
  116. self.model._meta.object_name, self.ct_field
  117. ),
  118. hint=(
  119. "GenericForeignKeys must use a ForeignKey to "
  120. "'contenttypes.ContentType' as the 'content_type' field."
  121. ),
  122. obj=self,
  123. id='contenttypes.E003',
  124. )
  125. ]
  126. elif field.remote_field.model != ContentType:
  127. return [
  128. checks.Error(
  129. "'%s.%s' is not a ForeignKey to 'contenttypes.ContentType'." % (
  130. self.model._meta.object_name, self.ct_field
  131. ),
  132. hint=(
  133. "GenericForeignKeys must use a ForeignKey to "
  134. "'contenttypes.ContentType' as the 'content_type' field."
  135. ),
  136. obj=self,
  137. id='contenttypes.E004',
  138. )
  139. ]
  140. else:
  141. return []
  142. def get_content_type(self, obj=None, id=None, using=None):
  143. if obj is not None:
  144. return ContentType.objects.db_manager(obj._state.db).get_for_model(
  145. obj, for_concrete_model=self.for_concrete_model)
  146. elif id is not None:
  147. return ContentType.objects.db_manager(using).get_for_id(id)
  148. else:
  149. # This should never happen. I love comments like this, don't you?
  150. raise Exception("Impossible arguments to GFK.get_content_type!")
  151. def get_prefetch_queryset(self, instances, queryset=None):
  152. if queryset is not None:
  153. raise ValueError("Custom queryset can't be used for this lookup.")
  154. # For efficiency, group the instances by content type and then do one
  155. # query per model
  156. fk_dict = defaultdict(set)
  157. # We need one instance for each group in order to get the right db:
  158. instance_dict = {}
  159. ct_attname = self.model._meta.get_field(self.ct_field).get_attname()
  160. for instance in instances:
  161. # We avoid looking for values if either ct_id or fkey value is None
  162. ct_id = getattr(instance, ct_attname)
  163. if ct_id is not None:
  164. fk_val = getattr(instance, self.fk_field)
  165. if fk_val is not None:
  166. fk_dict[ct_id].add(fk_val)
  167. instance_dict[ct_id] = instance
  168. ret_val = []
  169. for ct_id, fkeys in fk_dict.items():
  170. instance = instance_dict[ct_id]
  171. ct = self.get_content_type(id=ct_id, using=instance._state.db)
  172. ret_val.extend(ct.get_all_objects_for_this_type(pk__in=fkeys))
  173. # For doing the join in Python, we have to match both the FK val and the
  174. # content type, so we use a callable that returns a (fk, class) pair.
  175. def gfk_key(obj):
  176. ct_id = getattr(obj, ct_attname)
  177. if ct_id is None:
  178. return None
  179. else:
  180. model = self.get_content_type(id=ct_id,
  181. using=obj._state.db).model_class()
  182. return (model._meta.pk.get_prep_value(getattr(obj, self.fk_field)),
  183. model)
  184. return (ret_val,
  185. lambda obj: (obj._get_pk_val(), obj.__class__),
  186. gfk_key,
  187. True,
  188. self.name)
  189. def is_cached(self, instance):
  190. return hasattr(instance, self.cache_attr)
  191. def __get__(self, instance, cls=None):
  192. if instance is None:
  193. return self
  194. # Don't use getattr(instance, self.ct_field) here because that might
  195. # reload the same ContentType over and over (#5570). Instead, get the
  196. # content type ID here, and later when the actual instance is needed,
  197. # use ContentType.objects.get_for_id(), which has a global cache.
  198. f = self.model._meta.get_field(self.ct_field)
  199. ct_id = getattr(instance, f.get_attname(), None)
  200. pk_val = getattr(instance, self.fk_field)
  201. try:
  202. rel_obj = getattr(instance, self.cache_attr)
  203. except AttributeError:
  204. rel_obj = None
  205. else:
  206. if rel_obj and (ct_id != self.get_content_type(obj=rel_obj, using=instance._state.db).id or
  207. rel_obj._meta.pk.to_python(pk_val) != rel_obj._get_pk_val()):
  208. rel_obj = None
  209. if rel_obj is not None:
  210. return rel_obj
  211. if ct_id is not None:
  212. ct = self.get_content_type(id=ct_id, using=instance._state.db)
  213. try:
  214. rel_obj = ct.get_object_for_this_type(pk=pk_val)
  215. except ObjectDoesNotExist:
  216. pass
  217. setattr(instance, self.cache_attr, rel_obj)
  218. return rel_obj
  219. def __set__(self, instance, value):
  220. ct = None
  221. fk = None
  222. if value is not None:
  223. ct = self.get_content_type(obj=value)
  224. fk = value._get_pk_val()
  225. setattr(instance, self.ct_field, ct)
  226. setattr(instance, self.fk_field, fk)
  227. setattr(instance, self.cache_attr, value)
  228. class GenericRel(ForeignObjectRel):
  229. """
  230. Used by GenericRelation to store information about the relation.
  231. """
  232. def __init__(self, field, to, related_name=None, related_query_name=None, limit_choices_to=None):
  233. super().__init__(
  234. field, to, related_name=related_query_name or '+',
  235. related_query_name=related_query_name,
  236. limit_choices_to=limit_choices_to, on_delete=DO_NOTHING,
  237. )
  238. class GenericRelation(ForeignObject):
  239. """
  240. Provide a reverse to a relation created by a GenericForeignKey.
  241. """
  242. # Field flags
  243. auto_created = False
  244. many_to_many = False
  245. many_to_one = False
  246. one_to_many = True
  247. one_to_one = False
  248. rel_class = GenericRel
  249. def __init__(self, to, object_id_field='object_id', content_type_field='content_type',
  250. for_concrete_model=True, related_query_name=None, limit_choices_to=None, **kwargs):
  251. kwargs['rel'] = self.rel_class(
  252. self, to,
  253. related_query_name=related_query_name,
  254. limit_choices_to=limit_choices_to,
  255. )
  256. kwargs['blank'] = True
  257. kwargs['on_delete'] = models.CASCADE
  258. kwargs['editable'] = False
  259. kwargs['serialize'] = False
  260. # This construct is somewhat of an abuse of ForeignObject. This field
  261. # represents a relation from pk to object_id field. But, this relation
  262. # isn't direct, the join is generated reverse along foreign key. So,
  263. # the from_field is object_id field, to_field is pk because of the
  264. # reverse join.
  265. super().__init__(to, from_fields=[object_id_field], to_fields=[], **kwargs)
  266. self.object_id_field_name = object_id_field
  267. self.content_type_field_name = content_type_field
  268. self.for_concrete_model = for_concrete_model
  269. def check(self, **kwargs):
  270. errors = super().check(**kwargs)
  271. errors.extend(self._check_generic_foreign_key_existence())
  272. return errors
  273. def _is_matching_generic_foreign_key(self, field):
  274. """
  275. Return True if field is a GenericForeignKey whose content type and
  276. object id fields correspond to the equivalent attributes on this
  277. GenericRelation.
  278. """
  279. return (
  280. isinstance(field, GenericForeignKey) and
  281. field.ct_field == self.content_type_field_name and
  282. field.fk_field == self.object_id_field_name
  283. )
  284. def _check_generic_foreign_key_existence(self):
  285. target = self.remote_field.model
  286. if isinstance(target, ModelBase):
  287. fields = target._meta.private_fields
  288. if any(self._is_matching_generic_foreign_key(field) for field in fields):
  289. return []
  290. else:
  291. return [
  292. checks.Error(
  293. "The GenericRelation defines a relation with the model "
  294. "'%s.%s', but that model does not have a GenericForeignKey." % (
  295. target._meta.app_label, target._meta.object_name
  296. ),
  297. obj=self,
  298. id='contenttypes.E004',
  299. )
  300. ]
  301. else:
  302. return []
  303. def resolve_related_fields(self):
  304. self.to_fields = [self.model._meta.pk.name]
  305. return [(self.remote_field.model._meta.get_field(self.object_id_field_name), self.model._meta.pk)]
  306. def _get_path_info_with_parent(self):
  307. """
  308. Return the path that joins the current model through any parent models.
  309. The idea is that if you have a GFK defined on a parent model then we
  310. need to join the parent model first, then the child model.
  311. """
  312. # With an inheritance chain ChildTag -> Tag and Tag defines the
  313. # GenericForeignKey, and a TaggedItem model has a GenericRelation to
  314. # ChildTag, then we need to generate a join from TaggedItem to Tag
  315. # (as Tag.object_id == TaggedItem.pk), and another join from Tag to
  316. # ChildTag (as that is where the relation is to). Do this by first
  317. # generating a join to the parent model, then generating joins to the
  318. # child models.
  319. path = []
  320. opts = self.remote_field.model._meta
  321. parent_opts = opts.get_field(self.object_id_field_name).model._meta
  322. target = parent_opts.pk
  323. path.append(PathInfo(self.model._meta, parent_opts, (target,), self.remote_field, True, False))
  324. # Collect joins needed for the parent -> child chain. This is easiest
  325. # to do if we collect joins for the child -> parent chain and then
  326. # reverse the direction (call to reverse() and use of
  327. # field.remote_field.get_path_info()).
  328. parent_field_chain = []
  329. while parent_opts != opts:
  330. field = opts.get_ancestor_link(parent_opts.model)
  331. parent_field_chain.append(field)
  332. opts = field.remote_field.model._meta
  333. parent_field_chain.reverse()
  334. for field in parent_field_chain:
  335. path.extend(field.remote_field.get_path_info())
  336. return path
  337. def get_path_info(self):
  338. opts = self.remote_field.model._meta
  339. object_id_field = opts.get_field(self.object_id_field_name)
  340. if object_id_field.model != opts.model:
  341. return self._get_path_info_with_parent()
  342. else:
  343. target = opts.pk
  344. return [PathInfo(self.model._meta, opts, (target,), self.remote_field, True, False)]
  345. def get_reverse_path_info(self):
  346. opts = self.model._meta
  347. from_opts = self.remote_field.model._meta
  348. return [PathInfo(from_opts, opts, (opts.pk,), self, not self.unique, False)]
  349. def value_to_string(self, obj):
  350. qs = getattr(obj, self.name).all()
  351. return str([instance._get_pk_val() for instance in qs])
  352. def contribute_to_class(self, cls, name, **kwargs):
  353. kwargs['private_only'] = True
  354. super().contribute_to_class(cls, name, **kwargs)
  355. self.model = cls
  356. setattr(cls, self.name, ReverseGenericManyToOneDescriptor(self.remote_field))
  357. # Add get_RELATED_order() and set_RELATED_order() to the model this
  358. # field belongs to, if the model on the other end of this relation
  359. # is ordered with respect to its corresponding GenericForeignKey.
  360. if not cls._meta.abstract:
  361. def make_generic_foreign_order_accessors(related_model, model):
  362. if self._is_matching_generic_foreign_key(model._meta.order_with_respect_to):
  363. make_foreign_order_accessors(model, related_model)
  364. lazy_related_operation(make_generic_foreign_order_accessors, self.model, self.remote_field.model)
  365. def set_attributes_from_rel(self):
  366. pass
  367. def get_internal_type(self):
  368. return "ManyToManyField"
  369. def get_content_type(self):
  370. """
  371. Return the content type associated with this field's model.
  372. """
  373. return ContentType.objects.get_for_model(self.model,
  374. for_concrete_model=self.for_concrete_model)
  375. def get_extra_restriction(self, where_class, alias, remote_alias):
  376. field = self.remote_field.model._meta.get_field(self.content_type_field_name)
  377. contenttype_pk = self.get_content_type().pk
  378. cond = where_class()
  379. lookup = field.get_lookup('exact')(field.get_col(remote_alias), contenttype_pk)
  380. cond.add(lookup, 'AND')
  381. return cond
  382. def bulk_related_objects(self, objs, using=DEFAULT_DB_ALIAS):
  383. """
  384. Return all objects related to ``objs`` via this ``GenericRelation``.
  385. """
  386. return self.remote_field.model._base_manager.db_manager(using).filter(**{
  387. "%s__pk" % self.content_type_field_name: ContentType.objects.db_manager(using).get_for_model(
  388. self.model, for_concrete_model=self.for_concrete_model).pk,
  389. "%s__in" % self.object_id_field_name: [obj.pk for obj in objs]
  390. })
  391. class ReverseGenericManyToOneDescriptor(ReverseManyToOneDescriptor):
  392. """
  393. Accessor to the related objects manager on the one-to-many relation created
  394. by GenericRelation.
  395. In the example::
  396. class Post(Model):
  397. comments = GenericRelation(Comment)
  398. ``post.comments`` is a ReverseGenericManyToOneDescriptor instance.
  399. """
  400. @cached_property
  401. def related_manager_cls(self):
  402. return create_generic_related_manager(
  403. self.rel.model._default_manager.__class__,
  404. self.rel,
  405. )
  406. def create_generic_related_manager(superclass, rel):
  407. """
  408. Factory function to create a manager that subclasses another manager
  409. (generally the default manager of a given model) and adds behaviors
  410. specific to generic relations.
  411. """
  412. class GenericRelatedObjectManager(superclass):
  413. def __init__(self, instance=None):
  414. super().__init__()
  415. self.instance = instance
  416. self.model = rel.model
  417. content_type = ContentType.objects.db_manager(instance._state.db).get_for_model(
  418. instance, for_concrete_model=rel.field.for_concrete_model)
  419. self.content_type = content_type
  420. self.content_type_field_name = rel.field.content_type_field_name
  421. self.object_id_field_name = rel.field.object_id_field_name
  422. self.prefetch_cache_name = rel.field.attname
  423. self.pk_val = instance._get_pk_val()
  424. self.core_filters = {
  425. '%s__pk' % self.content_type_field_name: content_type.id,
  426. self.object_id_field_name: self.pk_val,
  427. }
  428. def __call__(self, *, manager):
  429. manager = getattr(self.model, manager)
  430. manager_class = create_generic_related_manager(manager.__class__, rel)
  431. return manager_class(instance=self.instance)
  432. do_not_call_in_templates = True
  433. def __str__(self):
  434. return repr(self)
  435. def _apply_rel_filters(self, queryset):
  436. """
  437. Filter the queryset for the instance this manager is bound to.
  438. """
  439. db = self._db or router.db_for_read(self.model, instance=self.instance)
  440. return queryset.using(db).filter(**self.core_filters)
  441. def get_queryset(self):
  442. try:
  443. return self.instance._prefetched_objects_cache[self.prefetch_cache_name]
  444. except (AttributeError, KeyError):
  445. queryset = super().get_queryset()
  446. return self._apply_rel_filters(queryset)
  447. def get_prefetch_queryset(self, instances, queryset=None):
  448. if queryset is None:
  449. queryset = super().get_queryset()
  450. queryset._add_hints(instance=instances[0])
  451. queryset = queryset.using(queryset._db or self._db)
  452. query = {
  453. '%s__pk' % self.content_type_field_name: self.content_type.id,
  454. '%s__in' % self.object_id_field_name: {obj._get_pk_val() for obj in instances}
  455. }
  456. # We (possibly) need to convert object IDs to the type of the
  457. # instances' PK in order to match up instances:
  458. object_id_converter = instances[0]._meta.pk.to_python
  459. return (queryset.filter(**query),
  460. lambda relobj: object_id_converter(getattr(relobj, self.object_id_field_name)),
  461. lambda obj: obj._get_pk_val(),
  462. False,
  463. self.prefetch_cache_name)
  464. def add(self, *objs, bulk=True):
  465. db = router.db_for_write(self.model, instance=self.instance)
  466. def check_and_update_obj(obj):
  467. if not isinstance(obj, self.model):
  468. raise TypeError("'%s' instance expected, got %r" % (
  469. self.model._meta.object_name, obj
  470. ))
  471. setattr(obj, self.content_type_field_name, self.content_type)
  472. setattr(obj, self.object_id_field_name, self.pk_val)
  473. if bulk:
  474. pks = []
  475. for obj in objs:
  476. if obj._state.adding or obj._state.db != db:
  477. raise ValueError(
  478. "%r instance isn't saved. Use bulk=False or save "
  479. "the object first." % obj
  480. )
  481. check_and_update_obj(obj)
  482. pks.append(obj.pk)
  483. self.model._base_manager.using(db).filter(pk__in=pks).update(**{
  484. self.content_type_field_name: self.content_type,
  485. self.object_id_field_name: self.pk_val,
  486. })
  487. else:
  488. with transaction.atomic(using=db, savepoint=False):
  489. for obj in objs:
  490. check_and_update_obj(obj)
  491. obj.save()
  492. add.alters_data = True
  493. def remove(self, *objs, bulk=True):
  494. if not objs:
  495. return
  496. self._clear(self.filter(pk__in=[o.pk for o in objs]), bulk)
  497. remove.alters_data = True
  498. def clear(self, *, bulk=True):
  499. self._clear(self, bulk)
  500. clear.alters_data = True
  501. def _clear(self, queryset, bulk):
  502. db = router.db_for_write(self.model, instance=self.instance)
  503. queryset = queryset.using(db)
  504. if bulk:
  505. # `QuerySet.delete()` creates its own atomic block which
  506. # contains the `pre_delete` and `post_delete` signal handlers.
  507. queryset.delete()
  508. else:
  509. with transaction.atomic(using=db, savepoint=False):
  510. for obj in queryset:
  511. obj.delete()
  512. _clear.alters_data = True
  513. def set(self, objs, *, bulk=True, clear=False):
  514. # Force evaluation of `objs` in case it's a queryset whose value
  515. # could be affected by `manager.clear()`. Refs #19816.
  516. objs = tuple(objs)
  517. db = router.db_for_write(self.model, instance=self.instance)
  518. with transaction.atomic(using=db, savepoint=False):
  519. if clear:
  520. self.clear()
  521. self.add(*objs, bulk=bulk)
  522. else:
  523. old_objs = set(self.using(db).all())
  524. new_objs = []
  525. for obj in objs:
  526. if obj in old_objs:
  527. old_objs.remove(obj)
  528. else:
  529. new_objs.append(obj)
  530. self.remove(*old_objs)
  531. self.add(*new_objs, bulk=bulk)
  532. set.alters_data = True
  533. def create(self, **kwargs):
  534. kwargs[self.content_type_field_name] = self.content_type
  535. kwargs[self.object_id_field_name] = self.pk_val
  536. db = router.db_for_write(self.model, instance=self.instance)
  537. return super().using(db).create(**kwargs)
  538. create.alters_data = True
  539. def get_or_create(self, **kwargs):
  540. kwargs[self.content_type_field_name] = self.content_type
  541. kwargs[self.object_id_field_name] = self.pk_val
  542. db = router.db_for_write(self.model, instance=self.instance)
  543. return super().using(db).get_or_create(**kwargs)
  544. get_or_create.alters_data = True
  545. def update_or_create(self, **kwargs):
  546. kwargs[self.content_type_field_name] = self.content_type
  547. kwargs[self.object_id_field_name] = self.pk_val
  548. db = router.db_for_write(self.model, instance=self.instance)
  549. return super().using(db).update_or_create(**kwargs)
  550. update_or_create.alters_data = True
  551. return GenericRelatedObjectManager