gpt4 book ai didi

Django:如何增加存储在 models.py/Database 中的计数器

转载 作者:行者123 更新时间:2023-12-02 08:09:20 25 4
gpt4 key购买 nike

我创建了一个小型计数器模型class Counter来存储小型调查应用程序已完成的次数。

我想从views.py中的def did()方法增加models.py中的SurveyWizardOneCounter

谁能告诉我该怎么做?

之前,我在计数器中使用了全局变量,但我发现它无法在网络应用程序中正常工作。我正在尝试学习如何在数据库中创建和存储计数器。任何帮助表示赞赏。

models.py

class Counter(models.Model):                
SurveyWizardOneCounter = models.SmallIntegerField()
TotalMaxCounter = models.SmallIntegerField()

views.py

from survey.models import Counter

def done(self, form_list, **kwargs):

Counter.SurveyWizardOneCounter += 1 # This is where I am trying to increment the counter

logger.debug('\n\n SurveyWizardOneCounter = %s', SurveyWizardOneCounter)

for form in form_list:
form.save()

return render(self.request, 'Return_to_AMT.html', {
'form_data': [form.cleaned_data for form in form_list],
})

最佳答案

由于您有 9 个不同的调查计数器,并且需要在提交调查时递增每个计数器,因此最好定义一个字段 survey_wizard_type ,其可能值为 survey_wizard_onesurvey_wizard_two 直到 survey_wizard_nine 以及具有默认值 0 的字段 survey_wizard_count,用于存储该特定的计数调查向导。那么您的数据库中将有 9 条 Counter 模型记录。

models.py

SURVEY_WIZARD_TYPE_CHOICES = ['survey_wizard_one', 'survey_wizard_two', 'survey_wizard_three', 'survey_wizard_four', 'survey_wizard_five', 'survey_wizard_six', 'survey_wizard_seven', 'survey_wizard_eight', 'survey_wizard_nine']

class Counter(models.Model):

survey_wizard_type = models.CharField(choices=SURVEY_WIZARD_TYPE_CHOICES)
survey_wizard_count = models.SmallIntegerField(default=0)
total_max_counter = models.SmallIntegerField()

然后在您的 views.py 中,您可以使用 get_or_create 方法来查找具有给定 survey_wizard_type< 的 Counter 对象,如有必要,创建一个。然后将 survey_wizard_count 加 1 并将该对象保存到数据库中。

views.py

from django.db.models import F
from survey.models import Counter

def done(self, form_list, **kwargs):

survey_counter = Counter.objects.get_or_create(survey_wizard_type= 'survey_wizard_x') # x can be any value from one to nine
survey_counter.survey_wizard_count = F('survey_wizard_count') + 1
survey_counter.save()

logger.debug('\n\n SurveyWizardOneCounter = %s', SurveyWizardOneCounter)

for form in form_list:
form.save()

return render(self.request, 'Return_to_AMT.html', {
'form_data': [form.cleaned_data for form in form_list],
})

编辑

Rahul 提供了解决方案,但这是我最终使用的代码

models.py

class Counter(models.Model):

SURVEY_WIZARD_TYPE_CHOICES = (
('SURVEY_WIZARD_ONE', 'survey_wizard_one'),
('SURVEY_WIZARD_TWO', 'survey_wizard_two'),
('SURVEY_WIZARD_THREE', 'survey_wizard_three'),
('SURVEY_WIZARD_FOUR', 'survey_wizard_four'),
('SURVEY_WIZARD_FIVE', 'survey_wizard_five'),
('SURVEY_WIZARD_SIX', 'survey_wizard_six'),
('SURVEY_WIZARD_SEVEN', 'survey_wizard_seven'),
('SURVEY_WIZARD_EIGHT', 'survey_wizard_eight'),
('SURVEY_WIZARD_NINE', 'survey_wizard_nine'),
)

survey_wizard_type = models.CharField(max_length=1000, choices=SURVEY_WIZARD_TYPE_CHOICES)
survey_wizard_count = models.SmallIntegerField(default=0)
total_max_counter = models.SmallIntegerField(default=0)

views.py

def done(self, form_list, **kwargs):

survey_counter = Counter.objects.get_or_create(survey_wizard_type= 'survey_wizard_one')[0] # x can be any value from one to nine
survey_counter.survey_wizard_count = F('survey_wizard_count') + 1
survey_counter.save()

for form in form_list:
form.save()

return render(self.request, 'Return_to_AMT.html', {
'form_data': [form.cleaned_data for form in form_list],
})

关于Django:如何增加存储在 models.py/Database 中的计数器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31322613/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com