简体   繁体   中英

Django renew field from database,calculated field in Database

Newby in django, have two question, can't find needed info.

1) I have database (SQLite) which have table scale_calibration and field weight. Other application rewrite value in field weight 1-2 times per second. Is there possibility in Django to renew this field without renew browser (F5)?

models.py:

from django.db import models

class Calibration(models.Model):
mean_weight = models.FloatField(editable=True)
hours_to_export = models.PositiveIntegerField(default=4, editable=True)
weight = models.FloatField(editable=True)

admin.py:

from django.contrib import admin
from .models import Calibration
# Register your models here.
admin.site.register(Calibration)

2) I try follow that link to make easy calculated field (that will be write to database when save), but i have no results and no error, don't understand where i did mistake.

models.py:

from django.db import models

class Calibration(models.Model):
    mean_weight = models.FloatField(editable=True)
    hours_to_export = models.PositiveIntegerField(default=4, editable=True)
    weight = models.FloatField(editable=True)
    calibration_factor = models.FloatField(editable=True)
    @property
    def get_calibration(self):
        return self.weight/self.mean_weight


def save(self, *args, **kwarg):
        self.calibration_factor = self.get_calibration()
        super(Calibration, self).save(*args, **kwarg)

Please help with advise.

As @david-alford mention, AJAX is a good solution to your problem. This simply writing JavaScript in your templates that make a request every n seconds and update your webpage, also you will need a new endpoint in your Django app that provides the update values from your model to this requests to be repeated.

If this sounds weird or complicated, take a look at some AJAX examples with Django , and feel free to ask more specific questions for clarifications.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM