Database API ManyToMany

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#Model
class Publication(models.Model):
title = models.CharField(max_length=30)
def __unicode__(self):
return self.title
class Article(models.Model):
headline = models.CharField(max_length=30)
publications = models.ManyToManyField(Publication)
def __unicode__(self):
return self.headline
#############################################################
# create ManyToMany
a1 = Article(headline='super news') # create article
a1.save() # save article and grand id number
p1 = Publication(name='hello world') # create publication
p1.save() # save and grand id
a1.publications.add(p1) # add relation
# or
p1.article_set.add(a1) # add relation
#########################################################################
# or short way
#########################################################################
a2 = Article(headline='NASA uses Python') # create new article
a2.save() # save article
a2.publications.create(title='Highlights') # crate publication and relate to article 2
# or
p2 = Publication(title='Name')
p2.save()
p2.article_set.create(headline='Article name')
###########################################################################
# relate to each other
a1.publications.add(p1,p2) # add if no exists publication 1 and 2 to article 1
a1.publications = [p1,p2] # set relation and remove all previouse relations
# or reverse
p1.article_set.add(a2,a1)
p1.article_set = [a1,a2]
###########################################################################
# Show related publications to article
a1 = Article.objects.get(pk=1)
a1.publications.all()
# or reverse
p1 = Publication.objects.get(pk=1)
p1.article_set.all()
# or
Article.objects.get(pk=1).publications.all()
# or reverse
Publication.objects.get(pk=1).article_set.all()
# or
Article.objects.filter(publications=2) # show all articles related to publication 2
# or reverse
Publication.objects.filter(article=2) # show all publication related to article 2
###########################################################################
# remove relation
a1 = Article.objects.get(pk=1)
a1.publications.clear()
# or
Article.objects.get(pk=1).publications.clear()
# or
Article.objects.get(pk=1).publications = []
# or
p1 = Publication.objects.get(pk=1)
Article.objects.get(pk=1).publications.remove(p1)
# or reversed
p1 = Publication.objects.get(pk=1)
p1.article_set.clear()
# or
Publication.objects.get(pk=1).article_set.clear()
# or
Publication.objects.get(pk=1).article_set = []
# or
a1 = Article.objects.get(pk=1)
Publication.objects.get(pk=1).article_set.remove(a1)
################################################################
# delete record
a1 = Article.objects.get(pk=1)
a1.publications.clear()
a1.delete()
# or reversed
p1 = Publication.objects.get(pk=1)
p1.article_set.clear()
p1.delete()