简体   繁体   中英

Make a Django model class whose one of the field value is calculated by other model's fields values and it must be present in my actual Database table

models.py

from django.db import models

class model_A(models.Model):
  value_1 = models.IntegerField()
  value_2 = models.FloatField()

class model_B(modles.Model):
  value_3 = models.FloatField()
  value_4 = (model_A.value_1 - model_B.value_2)

admin.py

from django.contrib import admin

# Register your models here.
from .models import model_A, model_B

model_lst = [model_A, model_B]

for i in model_lst:
    admin.site.register(i)

I want model_B to have value_4 as separate column in database table(model_B)

For this purpose, you can use computedfield:

https://pypi.org/project/django-computedfields/

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