使用django清理数据,在提交到db之前删除数据 [英] using django cleaned data pop to remove data before commit to db

查看:610
本文介绍了使用django清理数据,在提交到db之前删除数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有选择列表的表单。



当用户从 award_grant_type 选择列表中选择8888或9999的值时,我想要一些数据在表单数据提交到数据库之前,表单输入字段(用户可能已经输入数据到表单文本输入字段,然后选择8888或9999)可能存在或可能不存在。



所以我有以下model.py代码:

  ..... 
DISPLAY_ONLY_AWARD_AND_GRANT_DESCRIPTION_WITH_PROMPT = 8888
DISPLAY_ONLY_AWARD_AND_GRANT_DESCRIPTION_WITHOUT_PROMPT = 9999
.....

AWARD_GRANT_TYPES =(
(SELECT_AWARD_AND_GRANT_TYPE,_('Select Type')),
(AWARD,_('Award')),
(GRANT,_('Grant')),
(TRAVEL_GRANT,_('Travel Grant')),
(OTHER_AWARD ,_(其他奖),
(OTHER_GRANT,_(其他赠款)),
(WRITE_MY_OWN_AWARD_AND_GRANT_TYPE_DESCRIPTION,_(写我自己的类型说明)),#7777
(DISPLAY_ONLY_AWARD_AND_GRANT_DESCRIPTION_WITH_PROMPT,_('仅显示提示提示'))#8888
(DISPLAY_ONLY_AWARD_AND_GRANT_DESCRIPTION_WITHOUT_PROMPT,_('仅显示说明而不提示'))#9999


user = models.ForeignKey(User)
language_version = models.ForeignKey('LanguageVersion')
award_grant_type = models.PositiveIntegerField(choices = AWARD_GRANT_TYPES,default = SELECT_AWARD_AND_GRANT_TYPE,validators = [MinValueValidator(1)])
award_grant_type_description = models.CharField(null = True,blank = True,max_length = 250)
award_grant_date = models.DateField(null = True,blank = True)
award_grant_description = models.TextField = False,blank = False,max_length = 5000)

这是我的forms.py clean代码当用户从选择列表中选择了8888或9999时,删除 award_grant_type_description award_grant_date award_grant_typ e 在提交给db之前:

  def clean(self):

cd_agdf = super(AwardGrantDetailsForm,self).clean()

如果cd_agdf中的'award_grant_type':
如果cd_agdf ['award_grant_type'] =='':
self._errors ['award_grant_type'] = self.error_class([_(你必须选择一个类型)])

elif cd_agdf ['award_grant_type'] == 8888或cd_agdf ['award_grant_type '] == 9999:
#当奖励授予类型仅需要最少数据时,删除输入的值。
self.cleaned_data.pop('award_grant_type_description',无)
self.cleaned_data.pop('award_grant_date',无)
else:
....
返回cd_agdf

任何人都可以指出我做错了什么? code> award_grant_type_description 和 award_grant_date 在表单数据提交到数据库之前不会被删除。



编辑/更新



仅在现有记录更新时才会出现此问题。新记录在将表单保存到数据库之前删除所需的数据。当现有记录具有日期字段作为数据库记录的一部分,并且 award_grant_type 从更改为1到8888或9999时,则 award_grant_date 不会从数据库中删除。我不知道为什么。



第二次编辑



我有发表了相关主题此处

解决方案

我终于想到了这个问题。



< >当我将值包含在引号中时,问题已解决。



以下是我使用的示例代码:

  elif cd_agdf ['award_grant_type'] =='8888'或cd_agdf ['award_grant_type'] =='9999':

我希望这可以帮助某人。


I have a form with a select list.

When the user selects a value of 8888 or 9999 from the award_grant_type select list, I want some of the data that may or may not exist in the form input fields (the user may have entered data into the form text input fields and then selected 8888 or 9999) to be deleted before the form data is commited to the database.

So I have the following model.py code:

.....
DISPLAY_ONLY_AWARD_AND_GRANT_DESCRIPTION_WITH_PROMPT = 8888
DISPLAY_ONLY_AWARD_AND_GRANT_DESCRIPTION_WITHOUT_PROMPT = 9999
.....

AWARD_GRANT_TYPES = (
    (SELECT_AWARD_AND_GRANT_TYPE, _('Select Type')),
    (AWARD, _('Award')),
    (GRANT, _('Grant')),
    (TRAVEL_GRANT, _('Travel Grant')),
    (OTHER_AWARD, _('Other Award')),
    (OTHER_GRANT, _('Other Grant')),
    (WRITE_MY_OWN_AWARD_AND_GRANT_TYPE_DESCRIPTION, _('Write my own Type description')),  #7777
    (DISPLAY_ONLY_AWARD_AND_GRANT_DESCRIPTION_WITH_PROMPT, _('Display only Description with prompt')),  #8888
    (DISPLAY_ONLY_AWARD_AND_GRANT_DESCRIPTION_WITHOUT_PROMPT, _('Display only Description without prompt')) #9999
)

user = models.ForeignKey(User)
language_version = models.ForeignKey('LanguageVersion')
award_grant_type = models.PositiveIntegerField(choices=AWARD_GRANT_TYPES, default=SELECT_AWARD_AND_GRANT_TYPE, validators=[MinValueValidator(1)])
award_grant_type_description = models.CharField(null=True, blank=True, max_length=250)
award_grant_date = models.DateField(null=True, blank=True)
award_grant_description = models.TextField(null=False, blank=False, max_length=5000)

Here is my forms.py clean code that should remove the award_grant_type_description and award_grant_date fields when the user has selected 8888 or 9999 from the select list award_grant_type before being committed to the db:

def clean(self):

    cd_agdf = super(AwardGrantDetailsForm, self).clean()

    if 'award_grant_type' in cd_agdf:
        if cd_agdf['award_grant_type'] == '':
            self._errors['award_grant_type'] = self.error_class([_("You must select a Type.")])

        elif cd_agdf['award_grant_type'] == 8888 or cd_agdf['award_grant_type'] == 9999:
            #  remove the entered values when the award grant type only requires minimum data.
            self.cleaned_data.pop('award_grant_type_description', None)
            self.cleaned_data.pop('award_grant_date', None)
        else:
            ....
    return cd_agdf

Can anyone point out what I have done incorrectly? The award_grant_type_description and award_grant_date are not removed before the form data is committed to the db.

EDIT / UPDATE

This issue only occurs when the existing record is updated. A new record removes the data as required before the form is saved to the db. When an existing record has a date field as part of the db record and the award_grant_type is changed from say 1 to 8888 or 9999, then the award_grant_date is NOT removed from the db. I cannot figure out why.

2nd EDIT

I have posted a related thread here.

解决方案

I finally figured this issue out.

When I wrap the values in quotation marks, the issue is resolved.

Here is the example code I used that works:

 elif cd_agdf['award_grant_type'] == '8888' or cd_agdf['award_grant_type'] == '9999':

I hope that this will help someone.

这篇关于使用django清理数据,在提交到db之前删除数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆