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