#model function in template tag
1 messages ยท Page 1 of 1 (latest)
models.py: `class subjCategorieEx(models.Model):
subj = models.ForeignKey(subjects, on_delete=models.CASCADE)
categorie = models.ForeignKey(sbjCategorie, on_delete=models.CASCADE)
name = models.CharField(max_length=50, verbose_name="Exercise Name: ")
creator = models.ForeignKey(User, on_delete=models.CASCADE)
typeEx = models.CharField(max_length=25, verbose_name="Type of Exercise", default="list")
def calcLikes(self):
likesList = [int(i.like) for i in subjCategorieExLikes.objects.filter(excersise=self)]
return sum(likesList)`
views.py: for i in subjs: if i.lower() == subj.lower(): subj = subj.capitalize() return render(request, "categorie.html", {"exercises": excersiseList, "subjs": subjs, "subj": subj})
What do you mean, "get the remainder of a function"?
in my model i call a function that returns a value i need that value in a template tag in html
You can call methods on the model in templates. You can use {{ obj.getLikes }} in a template, for example, if obj is a model instance
i do a foreach i in subjCategorieEx and then i call i.likes (btw i changed the function name to likes) but my p tag is empyt
su the function is
so*
def likes():
likes = models.subjCategorieExLikes.objects.count()
return likes```
and in my html
{% for i in exercises %}
<!--somecode-->
<p>{{ i.likes }}</p>
<!--rest of code-->
{% endfor %}```
(the rest works only the function not )
Have you tested the function outside of the template?
There you go ๐
changed it to
@property
def likes(self):
return self.subjcategorieexlikes.count()```
how do i fix that cD
xD*
fixed it but now i get this error
'subjCategorieEx' object has no attribute 'subjCategorieExLikes'
class subjCategorieEx(models.Model):
subj = models.ForeignKey(subjects, on_delete=models.CASCADE)
categorie = models.ForeignKey(sbjCategorie, on_delete=models.CASCADE)
name = models.CharField(max_length=50, verbose_name="Exercise Name: ")
creator = models.ForeignKey(User, on_delete=models.CASCADE)
typeEx = models.CharField(max_length=25, verbose_name="Type of Exercise", default="list")
@property
def likes(self):
return self.subjCategorieExLikes.objects.count()
def __str__(self):
return f"name: {self.name} | type: {self.typeEx} | creator: {self.creator} |"
class subjCategorieExLikes(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name="User")
exercise = models.OneToOneField(subjCategorieEx, on_delete=models.CASCADE)```
Are you sure that's a OneToOneField? Each Like can only have one Exercise, and each Exercise can only have one Like??
Oh right, didn't think properly there
still getting the error
'subjCategorieEx' object has no attribute 'subjCategorieExLikes'
ok fixed it but it still doesn't show up in the html.
But when i print it in my terminal it does work
if i do
for i in models.subjCategorieEx.objects.filter(subj__name=f"{subj.lower()}", categorie__categorie=f"{categorie.lower().capitalize()}").values():
print(i.likes)```
it gives
AttributeError: 'dict' object has no attribute 'likes'
and if i do
for i in models.subjCategorieEx.objects.all():
print(i.likes)```
it works
but i need only the values of the likes in the specific categorie
When you call .values() on a queryset, you no longer have proper model instances. You get a dictionary for each instead, with just the fields you asked for. You won't be able to call model functions on those dictionaries, because they're not model instances.
thanks