With the following code I want to return the count this works...
def contact_count(self):
return len(self.contacts)
However, if the len is 0 is returns blank and I want it to be 0.
So I have tried
def contact_count(self):
if len(self.contacts) == 0:
return 0
else:
return len(self.contacts)
Is there a better way I should be handling this?
UPDATE:
So to update my question as asked this is how contacts is defined...
models.py
class Groups(models.Model):
name = models.CharField(max_length=60)
def contact_count(self):
return len(self.contacts)
class Contacts(models.Model):
first_name = models.CharField(max_length=60)
last_name = models.CharField(max_length=60)
#FK
groups = models.ManyToManyField(Groups, related_name='contacts')
As you can see if comes from the related_name
contactsdefined?return 0 if not self.contact else len(self.contacts)although it's only a matter of style really as what you have works