当前位置: 首页>>代码示例>>Python>>正文


Python Answer.field_id方法代码示例

本文整理汇总了Python中models.Answer.field_id方法的典型用法代码示例。如果您正苦于以下问题:Python Answer.field_id方法的具体用法?Python Answer.field_id怎么用?Python Answer.field_id使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在models.Answer的用法示例。


在下文中一共展示了Answer.field_id方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: save

# 需要导入模块: from models import Answer [as 别名]
# 或者: from models.Answer import field_id [as 别名]
    def save(self, collection=None):
        """    
        Saves the validated, cleaned form data. If a submission already exists,
        the new data will be merged over the old data.
        """
        
        # TODO: think about adding an "overwrite" argument to this function, default of False,
        # which will determine if an error should be thrown if the submission object already
        # exists, or if we should trust the data and overwrite the previous submission.

        # If there is no submission object, then this should just be a normal
        # Django form.  No need to call the save method, so we will raise an exception
        if not hasattr(self, 'submission'):
            raise LookupError("There is no submission object.  Are your creating the form with 'retrun_class=True'? If so, no need to call save.")

        if not self.cleaned_data:
            raise LookupError("The is_valid() method must be called before saving a form")
        
        # Slightly evil, do type checking to see if submission is a Submission object or string
        # If Submission object is a slug
        if isinstance(self.submission, str) or isinstance(self.submission, unicode):
            submission_slug = self.submission
            
            # Get or create the object
            self.submission, was_created = Submission.objects.get_or_create(slug=submission_slug, collection=collection)
            
        # Otherwise it should be a submission model object, if not raise
        elif not isinstance(self.submission, Submission):
            raise AttributeError('Submission %s is not a valid submission object.' % self.submission)
        

        # We now have a submission object, so let's update the last_modified field
        self.submission.last_modified = datetime.datetime.now()
        self.submission.save()
        
        # Delete Answers if they exist and start over ;)
        Answer.objects.select_related(
            'submission',
            'data_from',
            'field').filter(
                        data_form__slug=self.slug,
                        submission=self.submission).delete()
        
        # If answers don't exist, create the records without answers so that 
        # we can populate them later and save the relationships 
        # This is more efficient then using the ORM
        #if not answers:
            
        field_keys = []
        answers = []
        for key in self.fields.keys():
            # Mangle the key into the DB form, then get the right Field
            field_keys.append(_field_for_db(key))
        
        # Get All Fields
        fields = self.query_data['fields_list']
        
        for field in fields:
            # save the answer only if the field is in the form POST
            if field['slug'] in field_keys:
                # Create a new answer object
                answer = Answer()
                answer.submission = self.submission
                answer.data_form = self.query_data['dataform_query']
                answer.field_id = field['id']
                answers.append(answer)
        
        # Update the answers
        insert_many(answers)    
        
        # Get Answers again so that we have the pks
        answers = Answer.objects.select_related('submission', 'data_from', 'field').filter(data_form__slug=self.slug, submission=self.submission)
 
            
        # Get All possible choices from form models dict
        choices = self.query_data['choice_query']
        
        # Setup answer list so we can do a bulk update
        answer_objects = []

        #Delete choice relations
        delete_many(answers, table='dataforms_answer_choice')
        
        # We know answers exist now, so update them if needed.            
        for answer in answers:
            answer_obj, choice_relations = self._prepare_answer(answer, choices)
            answer_objects.append(answer_obj)

            if choice_relations:
                for choice_relation in choice_relations:
                    answer.choice.add(choice_relation)
        
        # Update the answers
        update_many(answer_objects, fields=['value'])
            
                    
        # Return a submission so the collection or form can have this.
        return self.submission
开发者ID:douggwaite,项目名称:django-dataforms,代码行数:100,代码来源:forms.py


注:本文中的models.Answer.field_id方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。