0
我無法弄清楚如何獲得manytomanyfield選擇的選項數量獲取models.ManyToManyField選擇的選項數
我的代碼:
class Author(models.Model):
first_name = models.CharField(max_length=30)
def __unicode__(self):
return u'%s' % (self.first_name)
class Book(models.Model):
title = models.CharField(max_length=100)
authors = models.ManyToManyField(Author)
authorsnumbers = models.IntegerField()
def __unicode__(self):
return self.title
def save(self, *args, **kwargs):
if not self.id:
self.authorsnumbers = self.authors.count()
super(Event, self).save(*args, **kwargs)
如果我選擇2名的作者,我想authorsnumber是' 2',但如何?
是的,但這是一樣的...但我想保存authorsnumber變量。和.authors.count()不起作用 – Doc