django - Avoiding existence errors -
def clean_bank_account(self): import ipdb; ipdb.set_trace() ssn = self.form.cleaned_data.get('ssn') customer = customerprofile.objects.filter(ssn=ssn) bank_account = self.form.cleaned_data.get('bank_account') bank = self.form.cleaned_data.get('bank') bank_transit = self.form.cleaned_data.get('bank_transit') qs = financialprofile.objects.filter( bank=bank, bank_transit=bank_transit, bank_account=bank_account) if customer.count() == 1: cust in customer: qs = qs.exclude(customer_id=cust.id) if qs.count() > 0: # concatenation of bank transit, bank account , bank # number must unique. hence, following message # displayed if in use. raise validationerror( _('the bank, bank transit , bank in use.') ) if bank not in (none, ''): # check bank account format specific banks length = settings.loanwolf_bank_accounts_length.get(bank) if length: if bank_transit not in (none, ''): if not bank_account.isnumeric() or length != len(bank_account): raise validationerror( _('bank account number must contain %d digits') % length # noqa ) else: raise validationerror( _('cannot validate bank account without valid bank transit') # noqa ) return bank_account
i improve structure of code. can't customerprofile.objects.get(ssn=ssn)
, because give me error if object doesn't exist. instead decided create list filter returns me queryset. lines
if customer.count() == 1: cust in customer: qs = qs.exclude(customer_id=cust.id)
will exclude customer id if there exists 1 customer in list. @ beginning, thought remove loop , using customer, can't since queryset. tought there way improve part of code?
the loop can replaced in filter
qs = qs.exclude(customer_id__in=[cust.id cust in customer])
Comments
Post a Comment