2015-11-11 100 views
3

我正在嘗試創建使用我的API創建新用戶的視圖。 我爲我的用戶使用自定義模型,並創建了一個名爲Profile的模型來管理與認證無關的數據。Django Rest Framework創建用戶和用戶配置文件

我是Django世界的新手,可能相當困難。

這裏是我的models.py

class UserManager(BaseUserManager): 
    def create_user(self, email, password=None): 
     if not email: 
      raise ValueError('User must have an email address') 

     user = self.model(
      email = self.normalize_email(email), 
     ) 
     user.set_password(password) 
     user.save() 
     return user 

    def create_superuser(self, email, password): 
     user = self.create_user(email, password=password) 
     user.is_admin = True 
     user.save() 
     return user 


class User(AbstractBaseUser): 
    objects = UserManager() 
    email = models.EmailField(unique=True, db_index=True) 
    created = models.DateTimeField('created', auto_now_add=True) 
    modified = models.DateTimeField(auto_now=True) 

    is_active = models.BooleanField('active', default=True) 
    is_admin = models.BooleanField('admin', default=False) 

    USERNAME_FIELD = 'email' 

    ordering = ('created',) 

    def is_staff(self): 
     return self.is_admin 

    def has_perm(self, perm, obj=None): 
     return True 

    def has_module_perms(self, app_label): 
     return True 

    def get_short_name(self): 
     return self.email 

    def get_full_name(self): 
     return self.email 

    def __unicode__(self): 
     return self.email 


class Profile(models.Model): 
    GENDER = (
     ('M', 'Homme'), 
     ('F', 'Femme'), 
    ) 

    user = models.OneToOneField(settings.AUTH_USER_MODEL) 
    first_name = models.CharField(max_length=120, blank=False) 
    last_name = models.CharField(max_length=120, blank=False) 
    gender = models.CharField(max_length=1, choices=GENDER) 
    zip_code = models.CharField(max_length=5, validators=[MinLengthValidator(5)], blank=False) 

    def __unicode__(self): 
     return u'Profile of user: {0}'.format(self.user.email) 


def create_profile(sender, instance, created, **kwargs): 
    if created: 
     Profile.objects.create(user=instance) 
post_save.connect(create_profile, sender=User) 


def delete_user(sender, instance=None, **kwargs): 
    try: 
     instance.user 
    except User.DoesNotExist: 
     pass 
    else: 
     instance.user.delete() 
post_delete.connect(delete_user, sender=Profile) 

這裏是我的serializers.py

class ProfileSerializer(serializers.ModelSerializer): 
    class Meta: 
     model = Profile 
     fields = ('first_name', 'last_name', 'gender', 'zip_code',) 


class UserSerializer(serializers.ModelSerializer): 
    profile = ProfileSerializer(required=True) 
    class Meta: 
     model = User 
     fields = ('url', 'email', 'profile', 'created',) 

這裏是views.py

class UserList(generics.ListCreateAPIView): 
    permission_classes = (IsAuthenticatedOrWriteOnly,) 
    serializer_class = UserSerializer 

    def get_queryset(self): 
     if self.request.user.is_staff: 
      return User.objects.all() 
     else: 
      return self.request.user 

    def post(self, request, format=None): 
     serializer = ProfileSerializer(data=request.data) 
     print serializer.__dict__ 
     if serializer.is_valid(): 
      print "valid" 
     return Response("placeholder", status=status.HTTP_201_CREATED) 

我的目標是能夠同時創建用戶和配置文件,同時能夠驗證所有內容,我如何實現這一目標?

回答

3

事實上,Django Rest Framework無法處理嵌套關係的工作,因此您必須自己實現這些方法。我會給你一些你的代碼應該是什麼樣子的例子。

您的看法:

class UserList(generics.ListCreateAPIView): 
    permission_classes = (IsAuthenticatedOrWriteOnly,) 
    serializer_class = UserSerializer 

    def post(self, request, format=None): 
     serializer = UserSerializer(data=request.data) 
     if serializer.is_valid(): 
      serializer.save() 
      return Response(serializer.data, status=status.HTTP_201_CREATED) 
     return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) 

現在,你的串行的save方法將調用一個方法create當你想要的時候,你要更新一個對象來創建一個對象,並update方法。因此,讓我們實施您的UserSerializercreate方法,該方法將創建配置文件和用戶。這裏是你的UserSerializer應該是什麼樣子一個簡單的例子:

class UserSerializer(serializers.ModelSerializer): 
    profile = ProfileSerializer(required=True) 
    class Meta: 
     model = User 
     fields = ('url', 'email', 'profile', 'created',) 

    def create(self, validated_data): 

     # create user 
     user = User.objects.create(
      url = validated_data['url'], 
      email = validated_data['email'], 
      # etc ... 
     ) 

     profile_data = validated_data.pop('profile') 
     # create profile 
     profile = Profile.objects.create(
      user = user 
      first_name = profile_data['first_name'], 
      last_name = profile_data['last_name'], 
      # etc... 
     ) 

     return user 

正如我所說的,這是一個例子,你必須完成它做你想做的事,但現在,你知道該怎麼做:)要定義更新過程中的行爲,請執行update方法。

+0

謝謝,這對我有點幫助,即使我已經開始瞭解這一點,創建方法很容易實現,但我必須說,更新不是。 因爲例如在我的UserSerializer中我有一個用於密碼和密碼確認的CharField,它們都是必需的,但是每當我想更新使用時,我需要再次提供這兩個密碼。 – soueuls

+0

請更新您的問題,然後讓您談論創作。你能更清楚地知道你想在更新方法中做什麼嗎? –

相關問題