relations.txt 3.4 KB

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