Я не могу понять ошибку здесь. У меня есть IntegerField (зарплата) в моей модели, тип поля которого переопределен в соответствующей форме модели. Для формы я сделал зарплату RegexField и добавил пользовательскую проверку, чтобы исключить запятые. Я также безуспешно пытался сделать поле формы модели CharField.
class Background_Check(models.Model):
user=models.ForeignKey(User, unique=True)
salary=models.IntegerField(blank=True,max_length=10)
class Background_CheckForm(forms.ModelForm):
salary=forms.RegexField(label=_("Salary"), max_length=10, regex=r'^[\d\s,]+',
#help_text = _("Required. 30 characters or fewer. Letters, digits and @/./+/-/_ only."),
error_messages = {'invalid': _("Please enter a valid salary")})
class Meta:
model=Background_Check
exclude=('user')
def clean_salary(self):
salary=str(self.cleaned_data["salary"])
re.sub(r'[,]','',salary)
return salary
Вот мой взгляд:
@login_required
def profile_settings(request):
page="account background"
user=User.objects.get(pk=request.user.id)
save_success=request.GET.get('save','')
try:
profile=user.background_check_set.all()[0]
profileform=Background_CheckForm(instance=profile)
except IndexError:
profile=''
profileform=Background_CheckForm()
if request.method=='POST':
#might be able to work get_or_create_object method here
if profile:
profileform=Background_CheckForm(request.POST,instance=profile)
else:
profileform=Background_CheckForm(request.POST)
if profileform.is_valid():
salary=profileform.cleaned_data['salary']
profile=profileform.save(commit=False)
profile.user=user
profile.save()
return HttpResponseRedirect("/account/profile/settings/?save=1")
else:
return render_to_response('website/profile_settings.html', {'page':page, 'profileform':profileform}, context_instance=RequestContext(request))
else:
return render_to_response("website/profile_settings.html", {'page':page,'profile':profile,'profileform':profileform,'save_success':save_success}, context_instance=RequestContext(request))
Когда я пытаюсь проверить форму модели, я получаю стандартное сообщение об ошибке для недопустимого IntegerField (это значение должно быть целым числом). Что тут происходит?
1 ответ
В вашем сообщении метод clean_salary() имеет неправильный отступ. Он должен быть частью вашего класса Background_CheckForm.
Если ваш код имеет такой же отступ, как и ваш пост, метод clean_salary() никогда не вызывается, и тогда вы, очевидно, получите стандартное сообщение об ошибке.
Похожие вопросы
Новые вопросы
django
Django - это серверная платформа веб-приложений с открытым исходным кодом, написанная на Python. Он разработан для сокращения усилий, необходимых для создания сложных веб-сайтов и веб-приложений, управляемых данными, с особым упором на меньшее количество кода, отсутствие избыточности и более явное, чем неявное.