[英]Django renew field from database,calculated field in Database
Newby in django, have two question, can't find needed info. Django 中的新手,有两个问题,找不到需要的信息。
1) I have database (SQLite) which have table scale_calibration and field weight. 1)我有数据库(SQLite),其中有表 scale_calibration 和字段权重。 Other application rewrite value in field weight 1-2 times per second.
其他应用程序每秒重写字段权重值 1-2 次。 Is there possibility in Django to renew this field without renew browser (F5)?
Django 是否有可能在不更新浏览器(F5)的情况下更新此字段?
models.py:模型.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:管理.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. 2)我尝试按照该链接制作简单的计算字段(保存时将写入数据库),但我没有结果也没有错误,不明白我哪里出错了。
models.py:模型.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.正如@david-alford 提到的,AJAX 是解决您问题的好方法。 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.这只是在您的模板中编写 JavaScript,每
n
秒发出一个请求并更新您的网页,您还需要在 Django 应用程序中创建一个新端点,该端点提供从您的模型到此重复请求的更新值。
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.如果这听起来很奇怪或复杂,请查看一些使用 Django 的 AJAX 示例,并随时提出更具体的问题以获得澄清。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.