第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如果名稱以 A Django 開頭,則取消選擇

如果名稱以 A Django 開頭,則取消選擇

繁星淼淼 2022-08-25 14:40:31
如果“人”的名稱以字母“a”開頭,我想刪除一個(gè)選擇選項(xiàng):基本上,如果People.nome或(Animal.pessoa)以字母“A”開頭,我想把選擇(2,“GATO”)去掉。from django.db import modelsfrom django.core.validators import RegexValidatorclass People(models.Model):    nome = models.CharField(max_length=200)    birthday_date = models.DateField()    cpf = models.CharField(max_length=11, validators=[RegexValidator(r'^\d{1,10}$')])    def __str__(self):        return '%s' % (self.nome)def cant_have_cat(self):    field_choices = [        (1, 'CACHORRO'),        (2, 'GATO'),        (3, 'OUTRO'),    ]    self.maiusculo = self.pessoa.upper    if self.maiusculo[0] == 'A':        self.tipo != 2    return field_choicesclass Animal(models.Model):    pessoa = models.ForeignKey(People, on_delete=models.CASCADE)    name = models.CharField(max_length=100)    tipo = models.IntegerField(choices=cant_have_cat)    custo = models.DecimalField(max_digits=7, decimal_places=2)    def __str__(self):        return '%s %s' % (self.pessoa, self.name)forms.pyclass AnimalForm(forms.ModelForm):    field_choices = [        (1, 'CACHORRO'),        (2, 'GATO'),        (3, 'OUTRO'),    ]    name = forms.CharField(max_length=100)    tipo = forms.ChoiceField(choices=field_choices)    custo = forms.DecimalField(max_digits=7, decimal_places=2)    class Meta:        prefix = 'animal'        model = Animal        fields = ('name', 'tipo', 'custo')    def clean(self):        people_name = self.People.nome        upper = people_name.upper()        if upper[0] == 'A':            Animal.tipo != 2
查看完整描述

1 回答

?
白衣染霜花

TA貢獻(xiàn)1796條經(jīng)驗(yàn) 獲得超10個(gè)贊

您可以覆蓋 您的 來處理邏輯。像這樣:__init__AnimalForm


class AnimalForm(forms.ModelForm):

    field_choices = [

        (1, 'CACHORRO'),

        (2, 'GATO'),

        (3, 'OUTRO'),

    ]

    field_choices_subset = [

        (1, 'CACHORRO'),

        (3, 'OUTRO'),

    ]


    def __init__(self, *args, **kwargs):

        super().__init__(*args, **kwargs)


        if kwargs.get("instance") and kwargs.get("instance").pessoa.nome.startswith("a"):

            self.fields["tipo"].choices = field_choices_subset


    name = forms.CharField(max_length=100)

    tipo = forms.ChoiceField(choices=field_choices)

    custo = forms.DecimalField(max_digits=7, decimal_places=2)


    class Meta:

        prefix = 'animal'

        model = Animal

        fields = ('name', 'tipo', 'custo')


查看完整回答
反對(duì) 回復(fù) 2022-08-25
  • 1 回答
  • 0 關(guān)注
  • 110 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)