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