Django ManyToManyField

930 단어 좋다
@python_2_unicode_compatible
class Publication(models.Model):
    title = models.CharField(max_length=30)

    def __str__(self):
        return self.title


@python_2_unicode_compatible
class Article(models.Model):
    headline = models.CharField(max_length=100)
    publications = models.ManyToManyField(Publication, related_name='publications_article')

    def __str__(self):
        return self.headline



#      
In [4]: p1 = Publication.objects.create(title='hello')

#     
In [5]: a1 = Article.objects.create(headline='django')

#                  publication_article   ,    
In [6]: p1.publications_article.all()
Out[6]: 

In [7]: p1.publications_article.add(a1)

#            
In [8]: p1.publications_article.all()
Out[8]: ]>

#            
In [9]: a1.publications.all()
Out[9]: ]>

좋은 웹페이지 즐겨찾기