[PYTHON] Django / Related objects (add, create, remove, clear, set) Summary

to write

I've been researching how to use add, create, remove, clear, and set several times. It is troublesome to read English each time, so I will summarize it in Japanese. Django Documentation / Related objects reference

Model to prepare, data


from django.db import models

class Reporter(models.Model):
    pass


class Article(models.Model):
    title = models.CharField(max_length=255, null=True)
    reporter = models.ForeignKey(Reporter, on_delete=models.CASCADE)

r1 = Reporter.objects.create(name='reporter1')
r2 = Reporter.objects.create(name='reporter2')

a1 = Article.objects.create(title='article1', reporter=r1)
a2 = Article.objects.create(title='article2', reporter=r1)
a3 = Article.objects.create(title='article3', reporter=r2)

About each method

add(*objs, bulk=True, through_defaluts=None) You can update the foreign key of the child table in the parent-child relationship by using the add method.

Example of use


>>> r1.article_set.all()
<QuerySet [<Article: Article object (1)>, <Article: Article object (2)>]>
>>> r2.article_set.all()
<QuerySet [<Article: Article object (3)>]>
>>> r2.article_set.add(a1,a2)
>>> r1.article_set.all()
<QuerySet []>
>>> r2.article_set.all()
<QuerySet [<Article: Article object (1)>, <Article: Article object (2)>, <Article: Article object (3)>]>

bulk=False

You can pass an unsaved object as an argument by using bulk = False. However, as bulk = False, queries will be issued for the number of objects to be added.


>>> r1.article_set.all()
<QuerySet [<Article: Article object (1)>, <Article: Article object (2)>]>
>>> new_a1 = Article()
>>> new_a2 = Article()
>>> r1.article_set.add(new_a1, new_a2, bulk=False)
>>> r1.article_set.all()
<QuerySet [<Article: Article object (1)>, <Article: Article object (2)>, <Article: Article object (4)>, <Article: Article object (5)>]
INSERT INTO `article` (`reporter_id`) VALUES (1)  # new_a1
INSERT INTO `article` (`reporter_id`) VALUES (1)  # new_a2

create(through_defaluts=None, **kwargs) By using the create method, you can create a new record with a small amount of description.


>>> r1.article_set.create()
>>> r1.article_set.all()
<QuerySet [<Article: Article object (1)>, <Article: Article object (2)>, <Article: Article object (3)>]>

This has similar results to:


>>> new_a = Article(reporter=r1)
>>> new_a.save()
>>> r1.article_set.all()

remove(*objs, bulk=True) remove () will update the ForeignKey to Null. It can be used only when ForeignKey (null = True). If you set Foreign Key (null = True) in the Model prepared above, you can use it as follows.


>>> r1.article_set.remove(a1, a2)

bulk=False You can also update records one by one by passing bulk = False as with add ().


>>> r1.article_set.remove(a1, a2, bulk=False)
>>> r1.article_set.all()
<QuerySet []>

If bulk = True,QuerySet.update ()is called, but if bulk = False, save () is called multiple times.

clear(bulk=True) clear () updates the Foreign Keys of all children to Null. Like clear (), it can be used only when ForeignKey (null = True).


>>> r1.article_set.clear()
>>> r1.article_set.all()
<QuerySet []>

bulk=False


>>> r1.article_set.clear(bulk=False)
>>> r1.article_set.all()
<QuerySet []>

As with remove (), save () is called multiple times.

set(objs, bulk=True, clear=False, through_defaults=None) GitHub link for set (OneToMany)

In set (), ʻadd (), remove (), and clear ()are running. If you passbulk = True / False as an argument, bulk` will be included in the argument of these methods running inside.

clear=False

>>> r1.article_set.set([a1, a3], bulk=False)

Is the same as:


>>> r1.article_set.remove(a2, bulk=False)
>>> r1.article_set.add(a1, a3, bulk=False)

clear=True


>>> r1.article_set.set([a1, a3], clear=True)

Is the same as:

>>> r1.article_set.clear()
>>> r1.article_set.add(a1, a3)

Summary

I've read the documentation about Django's Related objects again. In particular, my understanding of the options passed as arguments was ambiguous, so I hope it helps someone similar.

reference

Recommended Posts

Django / Related objects (add, create, remove, clear, set) Summary
Set method add remove clear
Django static file (static) related settings summary
Django Summary
Django Summary