关系对象参考

  • class RelatedManager
  • “关系管理器”是一个用于处理“一对多”和“多对多”关联关系的管理器。在以下两种情况用到:

  1. from django.db import models
  2.  
  3. class Reporter(models.Model):
  4. # ...
  5. pass
  6.  
  7. class Article(models.Model):
  8. reporter = models.ForeignKey(Reporter, on_delete=models.CASCADE)

在上面的例子中,以下方法会在管理器 reporter.article_set 中可用。

  1. class Topping(models.Model):
  2. # ...
  3. pass
  4.  
  5. class Pizza(models.Model):
  6. toppings = models.ManyToManyField(Topping)

在这个例子中,下文列出的方法在 topping.pizza_setpizza.toppings 中均可用。

  • add(*objs, bulk=True, through_defaults=None)
  • 将特定的模型对象加入关联对象集合。

举例:

  1. >>> b = Blog.objects.get(id=1)
  2. >>> e = Entry.objects.get(id=234)
  3. >>> b.entry_set.add(e) # Associates Entry e with Blog b.

上述例子中,由于有 ForeignKey 关联关系,才可以使用 QuerySet.update() 更新数据。这要求对象事先已经被保存在数据库内了。

你可以使用 bulk=False 参数让关系管理器通过调用 e.save() 来执行更新操作。

Using add() with a many-to-many relationship, however, will notcall any save() methods (the bulk argument doesn't exist), butrather create the relationships using QuerySet.bulk_create(). If you need to executesome custom logic when a relationship is created, listen to them2m_changed signal, which willtrigger pre_add and post_add actions.

Using add() on a relation that already exists won't duplicate therelation, but it will still trigger signals.

add() also accepts the field the relation points to as an argument.The above example can be rewritten as b.entry_set.add(234).

Use the through_defaults argument to specify values for the newintermediate model instance(s), ifneeded.

Changed in Django 2.2:The through_defaults argument was added.

  • create(through_defaults=None, **kwargs)
  • 创建一个新对象,保存至数据库,并将其放入关联对象集合内。返回刚创建的对象:
  1. >>> b = Blog.objects.get(id=1)
  2. >>> e = b.entry_set.create(
  3. ... headline='Hello',
  4. ... body_text='Hi',
  5. ... pub_date=datetime.date(2005, 1, 1)
  6. ... )
  7.  
  8. # No need to call e.save() at this point -- it's already been saved.

等同于(但更简单):

  1. >>> b = Blog.objects.get(id=1)
  2. >>> e = Entry(
  3. ... blog=b,
  4. ... headline='Hello',
  5. ... body_text='Hi',
  6. ... pub_date=datetime.date(2005, 1, 1)
  7. ... )
  8. >>> e.save(force_insert=True)

无需指定定义了模型间关系的关键字参数。在上述例子中,我们并未向 create() 传递参数 blog。Django 知道要将新 Entry 对象的 blog 字段设置为 b

Use the through_defaults argument to specify values for the newintermediate model instance, ifneeded.

Changed in Django 2.2:The through_defaults argument was added.

  • remove(*objs, bulk=True)
  • 从关系集合中移除特定的对象:
  1. >>> b = Blog.objects.get(id=1)
  2. >>> e = Entry.objects.get(id=234)
  3. >>> b.entry_set.remove(e) # Disassociates Entry e from Blog b.

Similar to add(), e.save() is called in the example aboveto perform the update. Using remove() with a many-to-manyrelationship, however, will delete the relationships usingQuerySet.delete() whichmeans no model save() methods are called; listen to them2m_changed signal if you wish toexecute custom code when a relationship is deleted.

Similarly to add(), remove() also accepts the field therelation points to as an argument. The above example can be rewrittenas b.entry_set.remove(234).

For ForeignKey objects, this method onlyexists if null=True. If the related field can't be set to None(NULL), then an object can't be removed from a relation withoutbeing added to another. In the above example, removing e fromb.entry_set() is equivalent to doing e.blog = None, and becausethe blog ForeignKey doesn't havenull=True, this is invalid.

For ForeignKey objects, this method acceptsa bulk argument to control how to perform the operation.If True (the default), QuerySet.update() is used.If bulk=False, the save() method of each individual modelinstance is called instead. This triggers thepre_save andpost_save signals and comes at theexpense of performance.

For many-to-many relationships, the bulk keyword argument doesn'texist.

  • clear(bulk=True)
  • 清空关系集合:
  1. >>> b = Blog.objects.get(id=1)
  2. >>> b.entry_set.clear()

Note this doesn't delete the related objects — it just disassociatesthem.

Just like remove(), clear() is only available onForeignKeys where null=True and it alsoaccepts the bulk keyword argument.

For many-to-many relationships, the bulk keyword argument doesn'texist.

  • set(objs, bulk=True, clear=False, through_defaults=None)
  • Replace the set of related objects:
  1. >>> new_list = [obj1, obj2, obj3]
  2. >>> e.related_set.set(new_list)

This method accepts a clear argument to control how to perform theoperation. If False (the default), the elements missing from thenew set are removed using remove() and only the new ones are added.If clear=True, the clear() method is called instead and thewhole set is added at once.

For ForeignKey objects, the bulkargument is passed on to add() and remove().

For many-to-many relationships, the bulk keyword argument doesn'texist.

Note that since set() is a compound operation, it is subject torace conditions. For instance, new objects may be added to the databasein between the call to clear() and the call to add().

Similarly to add(), set() also accepts the field therelation points to as an argument. The above example can be rewrittenas e.related_set.set([obj1.pk, obj2.pk, obj3.pk]).

Use the through_defaults argument to specify values for the newintermediate model instance(s), ifneeded.

Changed in Django 2.2:The through_defaults argument was added.

注解

Note that add(), create(), remove(), clear(), andset() all apply database changes immediately for all types ofrelated fields. In other words, there is no need to call save()on either end of the relationship.

If you use prefetch_related(),the add(), remove(), clear(), and set() methods clearthe prefetched cache.