Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django:如何增加存储在models.py/Database中的计数器_Django_Django Models_Django Views - Fatal编程技术网

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

Django:如何增加存储在models.py/Database中的计数器,django,django-models,django-views,Django,Django Models,Django Views,我创建了一个小型计数器模型类计数器,用于存储小型调查应用程序完成的次数 我想从my views.py中的def done()方法中增加my models.py中的SurveyWizardOneCounter 谁能告诉我怎么做 以前我使用全局变量作为计数器,我发现它不能与web应用程序正常工作。我正在尝试学习如何在数据库中创建和存储计数器。感谢您的帮助 型号.py class Counter(models.Model): SurveyWizardOneCou

我创建了一个小型计数器模型
类计数器
,用于存储小型调查应用程序完成的次数

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

谁能告诉我怎么做

以前我使用全局变量作为计数器,我发现它不能与web应用程序正常工作。我正在尝试学习如何在数据库中创建和存储计数器。感谢您的帮助

型号.py

class Counter(models.Model):                
    SurveyWizardOneCounter = models.SmallIntegerField()
    TotalMaxCounter = models.SmallIntegerField() 
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],            
    })  
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() 
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],            
    })   
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) 
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],            
    }) 
视图.py

class Counter(models.Model):                
    SurveyWizardOneCounter = models.SmallIntegerField()
    TotalMaxCounter = models.SmallIntegerField() 
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],            
    })  
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() 
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],            
    })   
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) 
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],            
    }) 

我假设您的
计数器
模型只存储一条记录

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)

由于您有9个不同的测量计数器,并且在提交测量时需要增加每个计数器,因此最好定义一个字段
survey\u wizard\u type
,其可能值为
survey\u wizard\u one
survey\u wizard\u two
till
survey\u wizard\u nine
和一个具有默认值
0
的字段
survey\u wizard\u count
,该字段存储特定调查向导的计数。您的数据库中将有9条记录用于
计数器
型号

型号.py

class Counter(models.Model):                
    SurveyWizardOneCounter = models.SmallIntegerField()
    TotalMaxCounter = models.SmallIntegerField() 
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],            
    })  
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() 
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],            
    })   
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) 
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],            
    }) 
然后,在
views.py
中,您可以使用
get\u或\u create
方法使用给定的
survey\u向导类型
查找
计数器
对象,必要时创建一个。然后将测量向导计数增加1,并将该对象保存到数据库中

视图.py

class Counter(models.Model):                
    SurveyWizardOneCounter = models.SmallIntegerField()
    TotalMaxCounter = models.SmallIntegerField() 
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],            
    })  
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() 
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],            
    })   
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) 
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],            
    }) 
编辑 Rahul提供了解决方案,但这是我最终使用的代码

型号.py

class Counter(models.Model):                
    SurveyWizardOneCounter = models.SmallIntegerField()
    TotalMaxCounter = models.SmallIntegerField() 
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],            
    })  
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() 
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],            
    })   
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) 
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],            
    }) 
视图.py

class Counter(models.Model):                
    SurveyWizardOneCounter = models.SmallIntegerField()
    TotalMaxCounter = models.SmallIntegerField() 
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],            
    })  
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() 
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],            
    })   
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) 
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],            
    }) 

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

from django.db.models import F
product = Product.objects.get(name='Venezuelan Beaver Cheese')
product.number_sold = F('number_sold') + 1
product.save()

django中的模型表示具有多行的数据库表,而不仅仅是单个数据存储。如果你还没有这样做,你真的应该通过django教程来了解模型是什么以及它是如何工作的。如果不是模型,我应该看什么来存储这样一个计数器变量?如果你需要,可以将它存储在模型中。该表是否只有一条记录?@Gocht将有9个这样的计数器,SurveyWizardOneCounter,SurveyWizardTwoCounter,等等。您确实需要创建一个带有计数器字段的
Survey
模型……感谢解决方案Rahul,我希望您不介意我添加了一些东西,以防其他人需要它!你的第一个建议是非原子性的(你可以得到一个竞赛条件)。第二种选择要好得多。请参阅Django推荐的方法: