2016-06-24 16 views
1

1pkある/api/provider/1/にPUTリクエストを送信するには私のカスタム・ユーザー・モデルDjangoの残りのフレームワークのカスタムユーザーモデルPUTリクエスト

ためPUT要求を送信している間、私は私が{"email": ["This field is required."] }

を取得 phone_number = 1234567890プットデータで、トラブルを抱えています

私はemailphone_number送信すると、私が手{"email": ["Provider with this email already exists."]}

モデル

class ProviderManager(BaseUserManager): 

    def _create_user(self, email, password, is_staff, is_superuser, **extra_fields): 
     '''Creates and saves a User with the given email and password''' 
     now = timezone.now() 

     if not email: 
      raise ValueError('Email can\'t be empty') 
     email = self.normalize_email(email) 
     user = self.model(email=email, is_active=True, is_superuser=is_superuser, 
          is_staff=is_staff, last_login=now, date_joined=now, 
          **extra_fields) 
     user.set_password(password) 
     user.save(using=self._db) 
     return user 

    def create_user(self, email, password, phone_number, **extra_fields): 
     extra_fields['phone_number'] = phone_number 
     return self._create_user(email, password, False, False, **extra_fields) 

    def create_user(self, email, password, phone_number, **extra_fields): 
     extra_fields['phone_number'] = phone_number 
     return self._create_user(email, password, True, True, **extra_fields) 


class Provider(AbstractBaseUser): 

    name = models.CharField(max_length=256, default='') 
    email = models.EmailField(max_length=256, unique=True, db_index=True) 
    phone_number = models.CharField(max_length=32, unique=True) 
    language = models.CharField(max_length=8, default='') 
    currency = models.CharField(max_length=8, default='') 

    USERNAME_FIELD = 'email' 
    REQUIRED_FIELDS = ['phone_number',] 

    objects = ProviderManager() 

    class Meta: 
     verbose_name = 'user' 
     verbose_name_plural = 'users' 

    def __unicode__(self): 
     return self.email 

    def get_full_name(self): 
     return self.name 

    def get_short_name(self): 
     return self.name 

    def email_user(self, subject, message, from_email=None, **kwargs): 
     send_mail(subject, message, from_email, [self.email]) 

シリアライザ

class ProviderSerializer(serializers.ModelSerializer): 

    class Meta: 
     model = get_user_model() 
     fields = ('id', 'name', 'email', 'phone_number', 'language', 'currency',) 

ビュー

@api_view(['GET', 'PUT', 'DELETE']) 
def provider_detail(request, provider_id): 
    '''Read, update or delete a provider''' 
    try: 
     provider = User.objects.get(pk=provider_id) 
    except User.DoesNotExist: 
     return Response(status=HTTP_404_NOT_FOUND) 

    print('GOT PROVIDER ' + provider.email) 

    if request.method == 'GET': 
     serializer = ProviderSerializer(provider) 
     return Response(serializer.data) 

    elif request.method == 'PUT': 
     serializer = ProviderSerializer(provider, data=request.data) 
     if serializer.is_valid(): 
      serializer.save() 
      return Response(serializer.data) 
     else: 
      return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) 

    elif request.method == 'DELETE': 
     provider.delete() 
     return Response(status=status.HTTP_204_NO_CONTENT) 

答えて

0

だけPATCHは、あなたが渡すことで更新できるようになるあなたはモデルのすべてのフィールドを提出する必要がPUTを行います1つのフィールド

関連する問題