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

时间:2015-07-09 15:58:56

标签: django django-models django-views

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

我希望在我的views.py中使用SurveyWizardOneCounter方法在models.py中增加def done()

谁能告诉我怎么做?

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

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],            
    })  

3 个答案:

答案 0 :(得分:4)

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

<强> models.py

Counter

然后在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的{​​{1}}对象,并在必要时创建一个。然后将Counter递增1并将该对象保存到数据库中。

<强> views.py

survey_wizard_type

修改

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

<强> models.py

survey_wizard_count

<强> 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],            
    })   

答案 1 :(得分:1)

我假设你的Counter模型只存储一条记录。

c = Counter.objects.get()
c.SurveyWizardOneCounter += 1
# Update another attributes if you need
c.save()

或者

from django.db.models import F

Counter.objects.filter().update(SurveyWizardOneCounter=F('SurveyWizardOneCounter')+1)

答案 2 :(得分:0)

您应该确保使用原子方法更新字段,上面的一些答案可能在更新和保存之间存在竞争条件。 Django documentation建议使用以下方法:

String [][] geo = {{"MD","NY","NJ","MA","ME","CA","MI","OR",},
        {"Detroit","Newark","Boston","Seattle"}};

    for (int j = 0; j < geo[0].length; j++)
    {
        if (geo[0][j].charAt(0) == 'M') {
            System.out.println(geo[0][j]);
        }
    }
}