繁体   English   中英

从Django中通过选择获取CharField的cleaned_data

[英]Getting the cleaned_data of a CharField with Choice from Django

我正在尝试检索属于使用Django中的选择的CharField的表单的数据。

我有以下model.py

class Transaccion(models.Model):
    ref = models.CharField(max_length=20, primary_key=True)
    fecha = models.DateField(default=timezone.now)
    usua = models.ForeignKey(User, on_delete=models.CASCADE, blank=True, null=True)
    monto = models.FloatField(max_length=50, blank=True, null=True)
    TYPE_TRANS = (
        ('d', 'Debito'),
        ('c', 'Credito'),
    )
    tipo = models.CharField(max_length=1, choices=TYPE_TRANS)
    LOAN_STATUS = (
        ('a', 'Aprobada'),
        ('e', 'Pendiente'),
        ('c', 'Rechazada'),
    )
    estado = models.CharField(max_length=1, choices=LOAN_STATUS, blank=True, default='e')
    TYPE_BANCO = (
        ('BBVA', 'Bco BBVA Provincial'),
        ('BOD', 'Banco Occidental de Descuento'),
        ('MER','Bco Mercantil')
    )
    bco = models.CharField(max_length=4, choices=TYPE_BANCO, blank=True)

以下form.py

class GestionarTransaccionForm(forms.ModelForm):
    class Meta:
        model = Transaccion

        fields = [
            'usua',
            'fecha',
            'bco',
            'ref',
            'monto',
            'tipo',
            'estado',
        ]
        widgets={
            'usua': forms.TextInput(attrs={'class': 'form-control', 'readonly': True}),
            'fecha': forms.TextInput(attrs={'class': 'form-control', 'readonly': True}),
            'bco': forms.Select(attrs={'class': 'form-control', 'readonly': True}),
            'ref': forms.TextInput(attrs={'class': 'form-control', 'readonly': True}),
            'monto': forms.TextInput(attrs={'class': 'form-contol', 'readonly': True}),
            'tipo': forms.Select(attrs={'class': 'form-control', 'readonly': True}),
            'estado': forms.Select(attrs={'class': 'form-control'}),
        }

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields['ref'].disabled = True
        self.fields['fecha'].disabled = True
        self.fields['usua'].disabled = True
        self.fields['monto'].disabled = True
        self.fields['tipo'].disabled = True
        self.fields['bco'].disabled = True

而这个views.py

class GestionarTransaccion(UpdateView):
    model = Transaccion
    form_class = GestionarTransaccionForm
    template_name = "administrador/gestionarT.html"
    success_url = reverse_lazy('transManager')

    def form_valid(self, form):
        instance = form.save(commit=False)
        u = User()
        if form.cleaned_data['estado']=='Aprobado':
            if form.cleaned_data['tipo']=='Credito':
                u.incrementarSaldo(form.cleaned_data['monto'], form.cleaned_data['usua']) 
            elif form.cleaned_data['tipo']=='Debito':
                u.disminuirSaldo(form.cleaned_data['monto'], form.cleaned_data['usua'])
        return super().form_valid(form)

处理此小代码段时出现问题:

        if form.cleaned_data['estado']=='Aprobado':
            if form.cleaned_data['tipo']=='Credito':
                u.incrementarSaldo(form.cleaned_data['monto'], form.cleaned_data['usua']) 
            elif form.cleaned_data['tipo']=='Debito':
                u.disminuirSaldo(form.cleaned_data['monto'], form.cleaned_data['usua'])

它需要在User内部使用几个方法(方法起作用,因为我没有条件地尝试了它们并且可以完美地工作了) ,但是对条件没有任何作用。 我怀疑cleaned_data格式不是我想的那样,但是尝试使用代码(“ d”而不是“ Debito”)根本没有任何作用。 关于如何使用它的任何想法?

编辑

变成

        if form.cleaned_data['estado']=='a':
            if form.cleaned_data['tipo']=='c':
                u.incrementarSaldo(form.cleaned_data['monto'], form.cleaned_data['usua']) 
            elif form.cleaned_data['tipo']=='d':
                u.disminuirSaldo(form.cleaned_data['monto'], form.cleaned_data['usua'])

它神奇地起作用了,尽管我尝试过但以前没有起作用。

存储在字段中的值是选择元组中的第一个元素,而不是第二个。

if form.cleaned_data['estado'] == 'a':
    if form.cleaned_data['tipo'] == 'c':

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM