簡體   English   中英

Django從數據庫更新字段,數據庫中的計算字段

[英]Django renew field from database,calculated field in Database

Django 中的新手,有兩個問題,找不到需要的信息。

1)我有數據庫(SQLite),其中有表 scale_calibration 和字段權重。 其他應用程序每秒重寫字段權重值 1-2 次。 Django 是否有可能在不更新瀏覽器(F5)的情況下更新此字段?

模型.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)

管理.py:

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

2)我嘗試按照該鏈接制作簡單的計算字段(保存時將寫入數據庫),但我沒有結果也沒有錯誤,不明白我哪里出錯了。

模型.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)

請幫忙指教。

正如@david-alford 提到的,AJAX 是解決您問題的好方法。 這只是在您的模板中編寫 JavaScript,每n秒發出一個請求並更新您的網頁,您還需要在 Django 應用程序中創建一個新端點,該端點提供從您的模型到此重復請求的更新值。

如果這聽起來很奇怪或復雜,請查看一些使用 Django 的 AJAX 示例,並隨時提出更具體的問題以獲得澄清。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM