Hi All,
I have a question regarding models in Django - Currently, I have 2 models, Leads and Leadentry, whereby Leadentry is related to Leads via a foreign key ID, now due to some of the functionality I’m adding to my application, it will be great if I can count the number of leadentry that are related to Leads in my models, in this case, Leadentry simply has the number of monthly payments that are expected on certain Lead, therefore, my question is how would I add a counter to Leads, such that I know if there are any entries in Leadentry corresponding to Leads?, is that possible?
Models
class Leads(models.Model):
project_id = models.BigAutoField(primary_key=True, serialize=False)
created_at = models.DateTimeField(auto_now_add=True)
expected_revenue = MoneyField(decimal_places=2,max_digits=14, default_currency='USD')
expected_licenses = models.IntegerField()
country = CountryField(blank_label='(select_country)')
status = models.CharField(choices=[('Open', 'Open'), ('Closed', 'Closed'), ('Canceled', 'Canceled'),
('Idle', 'Idle')
], max_length=10)
estimated_closing_date = models.DateField()
services = models.CharField(choices=[('Illumination Studies', 'Illumination Studies'),
('Training', 'Training'),('Survey Design Consultancy', 'Survey Design Consultancy'),
('Software License', 'Software License'),
('Software Development','Software Development')], max_length=40)
agent = models.ForeignKey(Profile, default='agent',on_delete=models.CASCADE)
company = models.ForeignKey(Company,on_delete=models.CASCADE)
point_of_contact = models.ForeignKey(Client, default='agent',on_delete=models.CASCADE)
updated_at = models.DateTimeField(auto_now=True)
application = models.CharField(choices=[('O&G','O&G'),('Renewables','Renewables'),('Mining','Mining'),
('Other','Other'),('CSS','CSS')],
default='O&G',max_length=20)
sub_category = models.CharField(choices=[('Wind','Wind'),('Geo-Thermal','Geo-Thermal'),('Solar','Solar'),
('Tidal','Tidal')], max_length=20, blank=True)
@property
def age_in_days(self):
today = date.today()
result = self.estimated_closing_date - today
return result.days
def __str__(self):
return f'{self.project_id}'
class LeadEntry(models.Model):
revenue = MoneyField(decimal_places=2,max_digits=14, default_currency='USD',blank=True)
date = models.DateField()
lead_id = models.ForeignKey(Leads,on_delete=models.CASCADE)
id = models.BigAutoField(primary_key=True, serialize=False)
probability = models.DecimalField(max_digits=2,
decimal_places=1,
default=0,
validators=[MaxValueValidator(1.0,'The value should be less than 1.0'),
MinValueValidator(0.0,'The value should be more than 0.0')]
)
stage = models.CharField(choices=[('Lead','Lead'),('Deal','Deal')], default='Lead',max_length=10, blank=True)
sales_order = models.CharField(
max_length=9,
validators=[RegexValidator(r'\d{4}.\d{4}', 'The value should be four digits, a character and four digits')],
default='0000-0000',
blank=True
)
Can I create an entry in my LeadModel or property that can sum the number of entries for each Lead?, so if there is nothing the count will be zero and otherwise it will return the number of entries