简体   繁体   中英

How to store calculated value in model field in Django?

I want to store two calculated fields in my extended user model Ai and Ei. these are calculated in views. my code is following

models. py

class User(AbstractUser):
Ai = models.TextField(null=True)
Ei = models.TextField(null=True)

def __str__(self):
    return self.username

and i am calculating Ai and Ei when user goes to link /join/so url.py is

url(r'^join/$', views.user_join),

In user join view

@login_required(login_url='/login/')
def user_join(request):

#Calculate Ai and Ei from Random

    User.Ai = str(Ai)
    User.Ei = str(Ei)
    User.save()

Now my problem is how do I store or Update values of Ai and Ei in the model for particular user

I get the answer now. Just required to request the user. following are changes.

@login_required(login_url='/login/')
def user_join(request):
U = request.user
#Calculate Ai and Ei from Random

U.Ai = str(Ai)
U.Ei = str(Ei)
U.save()

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