'How i can to pass parameter to model method in django template?
Hi everyone i have a question about django template and models method include function parameter.
example code in models.py
class Item(models.Model):
name = models.CharField(max_length=30)
price = models.IntegerField()
owner = models.ForeignKey('User',on_delete=models.CASCADE)
def get_something(self,requestuser): # <-- pass request.user here
// calculate something here
return "output"
in views.py
def assets(request):
return render(request, 'index.html', {'items':Item.objects.filter(owner=request.user)})
in template
{% for item in items %}
<h1>{{item.name}}</h1>
<h2>{{item.price}}</h2>
<h2>{{item.get_something(request.user) }}</h2> <!-- How to inject request.user to get_something Here -->
{% endfor %}
Solution 1:[1]
I found out what was my problem finally: We have a cache for our apis, and that prevented the react query to make a new request. so I sent a param to the api when I'm calling it as below:
const res = await api.get_vouchers({ orderby: 'created_at desc', top: '50', cache: false });
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Abraham Izadi |
