#model function in template tag

1 messages ยท Page 1 of 1 (latest)

bleak dune
#

How do I get the remainder of a function in a template tag?

#

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})

paper totem
#

What do you mean, "get the remainder of a function"?

bleak dune
#

in my model i call a function that returns a value i need that value in a template tag in html

paper totem
#

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

bleak dune
#

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 )

paper totem
#

Have you tested the function outside of the template?

bleak dune
#

aha

#

'subjCategorieExLikes' object has no attribute 'count'

paper totem
#

There you go ๐Ÿ˜Š

bleak dune
bleak dune
#

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)```
paper totem
#

Are you sure that's a OneToOneField? Each Like can only have one Exercise, and each Exercise can only have one Like??

bleak dune
#

Oh right, didn't think properly there

bleak dune
#

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

paper totem
#

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.

bleak dune
#

thanks