我有两个连接的模型。
模型1是 userprofiles ,模型2是事件
每个用户可以拥有多个事件......每个事件都可以激活或停用。
我在模板中有一个for循环,显示所有用户配置文件...但我还想显示每个用户有多少活动事件。
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]
在模板中我有我的循环
{% for kitchen_list in user_profiles %}
-- CODE --
{{ kitchen_list.events_set.count }} <--- WHERE I AM TRYING TO SHOW ACTIVE EVENTS COUNT
--CODE--
{% endfor %}
我的模特:
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
答案 0 :(得分:1)
你可以试试使用模型
模特中的:
class UserProfile(models.Model):
# You code here
@property
def active_events(self):
return self.user.events_set.filter(active='a').count()
模板中的:
{{ kitchen_list.userprofile.active_events }}
答案 1 :(得分:0)
而是在views.py而不是模板中进行计数,然后将值传递给模板。假设您在模型中很好地建立了外交关系,那么在视图中执行以下操作:
user_profiles = User.objects.filter(profile__user_profile_active='y').filter(is_active=True)
counts = user_profiles.events_set.count()
然后将其作为上下文传递。 或者您可以尝试在模型UserProfile中创建方法:
class UserProfile:
....
def events(self):
return self.events_set.count()