Django:将变量存储在数据库中,并在不同模块之间传递



在我的模型中,我试图计算我的用户年龄并为他们创建一个id代码,我希望这个信息保存在我的数据库中,但是"age"one_answers";id_code"字段在我的数据库中失踪。如果我更改函数的名称,则根本不计算年龄和id变量。

**accounts.models** 
class UserInformation(models.Model): 
name = models.CharField(max_length=250)
lastname = models.CharField(max_length=250)
phone = models.CharField(max_length=250)
birthday = models.DateField()
age = models.CharField(max_length=2)
id = models.CharField(max_length=250)   

def __str__(self):
self.name + '_' + self.lastname + '_' + str(self.birthday.year)
def age(self):
age = datetime.now().year - int(self.birthdate.year)
return age
def id_code(self):
id_code = self.name + '_' + self.lastname + '_' + int(self.birthday.year)
return id_code
**accounts.forms.py**
class UserInformationForm(forms.ModelForm):
class Meta:
model = UserInformation
fields = ('name', 'lastname', 'birthday', 'phone')
**accounts.views.py**
def add_information(request):
if request.method == 'POST':
form = UserInformationForm(request.POST, request.FILES)
if form.is_valid():
form.instance.user = request.user
form.save()           
return redirect('home')      
else:
form = UserInformationForm()
return render(request, 'add_information.html', {'form': form})

我也有另一个模型文件在另一个应用程序,我有两个测试我的用户。我想在我的数据库中保存答案,也在其他模型文件中创建的id代码并使用用户信息回答创建字符串名称在数据库中使用但它给了我错误"'ImportError: cannot import name 'name' from 'accounts.models' "即使我导入了模块:

**question.models.py**
from accounts.models import name
class QuestionOne(models.Model):
question_1a = models.CharField(max_length=250, choices=point)
question_2a = models.CharField(max_length=250, choices=point)
question_3a = models.CharField(max_length=250, choices=point)
id_code = models.CharField(max_length=250) 
def __str__(self):
return self.name + '_question_1'
class QuestionTwo(models.Model):
question_1b = models.CharField(max_length=250, choices=point)
question_2b = models.CharField(max_length=250, choices=point)
question_3b = models.CharField(max_length=250, choices=point)
id_code = models.CharField(max_length=250) 
def __str__(self):
return self.name + '_question_2'
**question.forms.py**
class QuestionOneForm(forms.ModelForm):
class Meta:
model = QuestionOne
fields = ('question_1a', 'question_2a', 'question_3a')
class QuestionTwoForm(forms.ModelForm):
class Meta:
model = QuestionOne
fields = ('question_1b', 'question_2b', 'question_3b')

最后,在我的html中,我想显示有多少问题表单已经完成,所以我添加了"quest__done"变量。不幸的是没有工作,在我的页面{{ quest_done }}只是一个空白

**question.views.py**
def question_one(request):
quest_done = 0
if request.method == 'POST':
form = QuestionOneForm(request.POST, request.FILES)
if form.is_valid():
quest_done += 1
form.instance.user = request.user           
form.save()
return redirect('home')      
else:
form = QuestionOneForm()
return render(request, 'quest.html', {'form': form, 'quest_done': quest_done})
def question_two(request):
quest_done = 0
if request.method == 'POST':
form = QuestionTwoForm(request.POST, request.FILES)
if form.is_valid():
quest_done += 1
form.instance.user = request.user           
form.save()
return redirect('home')      
else:
form = QuestionTwoForm()
return render(request, 'quest.html', {'form': form, 'quest_done': quest_done})
*html**
<div class="row">
<div class="col-sm-3">
<h6 class="mb-0">Question form completed:</h6>
</div>
<div class="col-sm-9 text-secondary">
{{ quest_done }} /2
</div>
</div>
Override save() method in model.
class UserInformation(models.Model): 
name = models.CharField(max_length=250)
lastname = models.CharField(max_length=250)
phone = models.CharField(max_length=250)
birthday = models.DateField()
age = models.CharField(max_length=2,default="")
id = models.CharField(max_length=250,default="") 

def save(self, *args, **kwargs):
if self.age ="":
self.age = datetime.now().year - int(self.birthdate.year)
self.save()
super(UserInformation, self).save(*args, **kwargs)

你可以在save()方法中对id做同样的操作。

相关内容

  • 没有找到相关文章

最新更新