relations.txt 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. =========================
  2. Related objects reference
  3. =========================
  4. .. currentmodule:: django.db.models.fields.related
  5. .. class:: RelatedManager
  6. A "related manager" is a manager used in a one-to-many or many-to-many
  7. related context. This happens in two cases:
  8. * The "other side" of a :class:`~django.db.models.ForeignKey` relation.
  9. That is::
  10. from django.db import models
  11. class Reporter(models.Model):
  12. # ...
  13. pass
  14. class Article(models.Model):
  15. reporter = models.ForeignKey(Reporter)
  16. In the above example, the methods below will be available on
  17. the manager ``reporter.article_set``.
  18. * Both sides of a :class:`~django.db.models.ManyToManyField` relation::
  19. class Topping(models.Model):
  20. # ...
  21. pass
  22. class Pizza(models.Model):
  23. toppings = models.ManyToManyField(Topping)
  24. In this example, the methods below will be available both on
  25. ``topping.pizza_set`` and on ``pizza.toppings``.
  26. These related managers have some extra methods:
  27. .. method:: add(obj1, [obj2, ...])
  28. Adds the specified model objects to the related object set.
  29. Example::
  30. >>> b = Blog.objects.get(id=1)
  31. >>> e = Entry.objects.get(id=234)
  32. >>> b.entry_set.add(e) # Associates Entry e with Blog b.
  33. .. method:: create(**kwargs)
  34. Creates a new object, saves it and puts it in the related object set.
  35. Returns the newly created object::
  36. >>> b = Blog.objects.get(id=1)
  37. >>> e = b.entry_set.create(
  38. ... headline='Hello',
  39. ... body_text='Hi',
  40. ... pub_date=datetime.date(2005, 1, 1)
  41. ... )
  42. # No need to call e.save() at this point -- it's already been saved.
  43. This is equivalent to (but much simpler than)::
  44. >>> b = Blog.objects.get(id=1)
  45. >>> e = Entry(
  46. ... blog=b,
  47. ... headline='Hello',
  48. ... body_text='Hi',
  49. ... pub_date=datetime.date(2005, 1, 1)
  50. ... )
  51. >>> e.save(force_insert=True)
  52. Note that there's no need to specify the keyword argument of the model
  53. that defines the relationship. In the above example, we don't pass the
  54. parameter ``blog`` to ``create()``. Django figures out that the new
  55. ``Entry`` object's ``blog`` field should be set to ``b``.
  56. .. method:: remove(obj1, [obj2, ...])
  57. Removes the specified model objects from the related object set::
  58. >>> b = Blog.objects.get(id=1)
  59. >>> e = Entry.objects.get(id=234)
  60. >>> b.entry_set.remove(e) # Disassociates Entry e from Blog b.
  61. For :class:`~django.db.models.ForeignKey` objects, this method only
  62. exists if ``null=True``. If the related field can't be set to ``None``
  63. (``NULL``), then an object can't be removed from a relation without
  64. being added to another. In the above example, removing ``e`` from
  65. ``b.entry_set()`` is equivalent to doing ``e.blog = None``, and because
  66. the ``blog`` :class:`~django.db.models.ForeignKey` doesn't have
  67. ``null=True``, this is invalid.
  68. .. method:: clear()
  69. Removes all objects from the related object set::
  70. >>> b = Blog.objects.get(id=1)
  71. >>> b.entry_set.clear()
  72. Note this doesn't delete the related objects -- it just disassociates
  73. them.
  74. Just like ``remove()``, ``clear()`` is only available on
  75. :class:`~django.db.models.ForeignKey`\s where ``null=True``.