2016-11-24 7 views
1

私はdjango-phonenumber-fieldバッテリーを私のdjangoアプリ(django 1.9、python 3.5)に使用しています。Djangoは "タイプエラーを修正できません"、SqliteとPostresqlは

次のように私は次の形式を持っている:私の開発環境で

class PhoneChangeForm(forms.Form): 

    phonenumber = PhoneNumberField(widget=PhoneNumberPrefixWidget(attrs={'placeholder': _('Phone number')}), 
            label=_("Enter phone")) 

    def clean_phonenumber(self): 
     users = User.objects.filter(profile__phone__iexact=self.cleaned_data['phonenumber']) 
     if users.count() == 0: 
      return self.cleaned_data['phonenumber'] 
     else: 
      raise forms.ValidationError(_("This phone is already in use.")) 

をこのフォームが正常に動作します。指定された電話番号を持つユーザーが存在する場合、ValidationErrorが発生します。私はここに頼むことにしましたので

Django Version:  1.9.6 
Exception Type:  ProgrammingError 
Exception Value: can't adapt type 'PhoneNumber' 

クリエイターは、最後のコメントに反応しない:私はエラーを次しているプロダクションに同じコードを移動してい

+0

実稼働環境でdjango-phonenumber-fieldをpip installしましたか?あなたはphonenumberでユーザーを格納するカスタムユーザーモデルを使用していますか? –

+0

確かに、それです! –

+1

実稼働環境でsqlite3を使用している場合は、sqlite3を文字列として格納しようとします。 http://stackoverflow.com/questions/6510529/django-cant-adapt-type-error-using-mutliple-databases-postgresql-8-4-and-sql –

答えて

0

プロダクション私はPostqlを開発Sqlite用に使用しています。 この[回答] [2]に基づいて、DBバックエンドに応じてDjangoのフィルタに何らかのバグがあります。 Sqliteを使用する場合:

User.objects.filter(profile__phone__iexact=self.cleaned_data['phonenumber']) 

は魅力的なように働いています。

Postrgresを使用する場合、「can not adapt type」がスローされます。値を文字列に明示的に変換すると、問題が解決します。

users = User.objects.filter(profile__phone__iexact=str(self.cleaned_data['phonenumber'])) 
関連する問題