You can add methods to your model class

class Container(models.Model):
    name = models.CharField(max_length=128)
    capacity = models.IntegerField()
    contains = models.IntegerField()

    def usage_percentage():
        return 100.0 * self.contains / self.capacity

And in your template

{% if object_list %}
    <table>
    {% for o in object_list %}
        <tr>
        <td>{{ o.name }}</td> <td>{{ o.capacity }}</td> <td>{{ o.contains
}}</td> <td> {{ o.usage_percentage }} </td>
        </tr>
    {% endfor %}
    </table>
{% endif %}

On Tue, Jun 9, 2015 at 11:03 PM, JLeary <sillymonkeysoftw...@gmail.com>
wrote:

> Hi.
> Total Django Newbie here... and pretty new to Python too.
> Anyway, I have this simple problem, and I've been reading documentation
> all afternoon but just can't come up with a simple solution.
>
> How can I display calculated values from the Model vales store in the
> database?
> Like, a percentage of two numbers?
>
> For instance;
>
> # myapp/models.py
> class Container(models.Model):
>     name = models.CharField(max_length=128)
>     capacity = models.IntegerField()
>     contains = models.IntegerField()
>
>
> python manage.py shell
> >>> Container(name="big bucket", capacity=100, contains=22).save()
> >>> Container(name="small bucket", capacity=50, contains=48).save()
> >>> Container(name="medium bucket", capacity=75, contains=13).save()
>
>
> # myapp/views.py
> from django.http import HttpResponse
> from django.template import RequestContext, loader
> from .models import Container
> def index(request):
>     object_list = Container.objects.all()
>     template = loader.get_template('myapp/index.html')
>     context = RequestContext(request, {
>         'object_list': object_list,
>     })
>     return HttpResponse(template.render(context))
>
>
> # myapp/templates/myapp/index.html
> {% if object_list %}
>     <table>
>     {% for o in object_list %}
>         <tr>
>         <td>{{ o.name }}</td> <td>{{ o.capacity }}</td> <td>{{ o.contains
> }}</td>
>         </tr>
>     {% endfor %}
>     </table>
> {% endif %}
>
> .
>
>
> So, all is fine when I create a simple view template to display the NAME,
> CAPACITY, and CONTAINS info for each object.
>
> What I really want... is the Percent-Full of each Container to be
> displayed.
> But, I'm stumped as to how I can do this.
>
> I've tried creating a sub-class of Container, that contained methods to
> return the percentages, but was unable to get it to work.
>
> Any input is appreciated.
> Thanks.
>
>  --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/386ecbdd-79e1-4646-b444-c5cd559ceb24%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/386ecbdd-79e1-4646-b444-c5cd559ceb24%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CALn3ei0w87PR0NPu3rU4xua_YLdwk190JitCuo%3Dz-AtW06hpWg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to