Reputation: 135
I have 2 models that are connected.
Model 1 is userprofiles and model 2 is events
each user can have multiple events... each event can be active or deactivated.
I have a for loop in the template showing all the userprofiles... but I also want to show how many active events each user has.
today = datetime.now().strftime('%Y-%m-%d')
perm = Permission.objects.get(codename='chef_user')
user_profiles = User.objects.filter(profile__user_profile_active='y').filter(is_active=True).filter(Q(groups__permissions=perm) | Q(user_permissions=perm)).distinct()[:6]
in the template I have my loop
{% for kitchen_list in user_profiles %}
-- CODE --
{{ kitchen_list.events_set.count }} <--- WHERE I AM TRYING TO SHOW ACTIVE EVENTS COUNT
--CODE--
{% endfor %}
my models:
class UserProfile(models.Model):
EVENT_TYPE = (('0', "Apartment"), ('1', "Home"),)
CURRENCY = (('R', "Rand"),)
OPTIONS = (('y', "Yes"),('n', "No"),)
OPTIONS_USER = (('y', "Yes"), ('n', "No"),('p', "Pending"),)
user = models.OneToOneField(User, related_name='profile')
phone_regex = RegexValidator(regex=r'^\+?1?\d{9,12}$', message="Phone number must be entered in the format: '+999999999'. Up to 15 digits allowed.")
phone = models.CharField(validators=[phone_regex], max_length=12, default='', blank=True) # validators should be a list
currency = models.CharField(max_length=50, blank=True, default='R', choices=CURRENCY)
bookingpermissions = models.CharField(max_length=100, blank=True, default='n', choices=OPTIONS)
title = models.CharField(max_length=50, default='', blank=True)
bio = models.TextField(max_length=500, default='', blank=True)
city = models.CharField(max_length=100, default='', blank=True)
longitude = models.CharField(max_length=100, default='na', blank=True)
latitude = models.CharField(max_length=100, default='na', blank=True)
image = ResizedImageField(size=[400, 400], crop=['middle', 'center'], upload_to='userprofile/static/images', default='', blank=True)
kitchen_type = models.CharField(max_length=50, blank=True, default='a', choices=EVENT_TYPE)
user_profile_active = models.CharField(max_length=1, default='n', blank=True, choices=OPTIONS_USER)
class Events(models.Model):
ACTIVE = (('d', "Deactivated"), ('e', "Expired"), ('a', "Active"), ('b', "Drafts"),)
ALCOHOL = (('0','No bring own alcohol'),('1','There will be complimentary wine pairing'))
user = models.ForeignKey(User, on_delete=models.CASCADE)
active = models.CharField(max_length=1, default='b', choices=ACTIVE)
title = models.CharField(max_length=50, blank=True, default='')
description = models.TextField(max_length=500, blank=True, default='')
date = models.DateField()
time = models.TimeField()
price = models.CharField(max_length=240, blank=True, default='')
seats = models.IntegerField()
alcohol_choice = models.CharField(max_length=1, default='n' ,choices=ALCOHOL)
starter = models.TextField(max_length=350, blank=True, default='')
main_menu = models.TextField(max_length=350, blank=True, default='')
dessert = models.TextField(max_length=350, blank=True, default='')
notes = models.TextField(max_length=350, blank=True, default='')
created_date = models.DateTimeField(auto_now_add=True)
modified_date = models.DateTimeField(auto_now=True)
def __unicode__(self):
return self.title
Upvotes: 0
Views: 79
Reputation:
you can try use model propery
in model:
class UserProfile(models.Model):
# You code here
@property
def active_events(self):
return self.user.events_set.filter(active='a').count()
in template:
{{ kitchen_list.userprofile.active_events }}
Upvotes: 1
Reputation: 374
Rather do it in the views.py instead of the template to count then pass the values to the template. Assuming you hva e set up the foreign relations well in the models then in the views do the following:
user_profiles = User.objects.filter(profile__user_profile_active='y').filter(is_active=True)
counts = user_profiles.events_set.count()
Then pass that as a context. Or you can try creating a method in your model UserProfile:
class UserProfile:
....
def events(self):
return self.events_set.count()
Upvotes: 0