2016-09-30 134 views
0

使用restframework進行用戶註冊 我需要在保存之前生成隨機密碼,我重寫模型中的保存方法,但是這種方式永遠不會運行並且需要丟棄消息密碼字段。 ¿我如何覆蓋這個?謝謝。覆蓋擴展AbstractBaseUser的django模型保存功能

# ViewSet. 
class AgentViewSet(viewsets.ModelViewSet): 
    queryset = Agents.objects.all() 
    serializer_class = AgentSerializer 
    permission_classes = (permissions.AllowAny,) 
    http_method_names = ['post'] 

# Model. 
class Agents(AbstractBaseUser): 
    oldkey = models.CharField(max_length=32, blank=True, null=True) 
    is_main_user = models.IntegerField(blank=True, null=True) 
    name = models.CharField(max_length=45, blank=True, null=True) 
    lastname = models.CharField(max_length=45, blank=True, null=True) 
    email1 = models.CharField(
     max_length=100, blank=True, null=True, unique=True 
    ) 
    email2 = models.CharField(max_length=100, blank=True, null=True) 
    city = models.CharField(max_length=200, blank=True, null=True) 
    country = models.ForeignKey(
     'bdetail.Countries', models.DO_NOTHING, db_column='country', 
     blank=True, null=True 
    ) 
    username = models.CharField(max_length=30, blank=True, null=True) 
    image = models.CharField(max_length=80, blank=True, null=True) 
    num_access = models.IntegerField(blank=True, null=True) 
    phone1 = models.CharField(max_length=20, blank=True, null=True) 
    phone2 = models.CharField(max_length=20, blank=True, null=True) 
    phone3 = models.CharField(max_length=20, blank=True, null=True) 
    whatsapp = models.CharField(max_length=20, blank=True, null=True) 
    permissions = models.CharField(
     max_length=10, blank=True, null=True, default='limited') 
    fb_profile_id = models.CharField(max_length=30, blank=True, null=True) 
    fb_access_token = models.TextField(blank=True, null=True) 
    clients = models.ForeignKey(
     'Clients', models.DO_NOTHING, blank=True, null=True 
    ) 
    last_login_time = models.IntegerField(blank=True, null=True) 
    about = models.TextField('Acerca de mi', blank=True) 

    USERNAME_FIELD = 'email1' 
    REQUIRED_FIELDS = ['name'] 
    objects = MyUserManager() 

    class Meta: 
     db_table = 'agents' 

    def get_full_name(self): 
     return self.name, ' ', self.lastname 

    def get_short_name(self): 
     return self.name 

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

    def save(self, *args, **kwargs): 
     raise Exception('ingres') 

    def has_module_perms(self, app_label): 
     "Does the user have permissions to view the app `app_label`?" 
     # Simplest possible answer: Yes, always 
     return True 

    def __unicode__(self): 
     return unicode(self.name) or u'' 

    @property 
    def is_staff(self): 
     "Is the user a member of staff?" 
     # Simplest possible answer: All admins are staff 
     return False 

回答

1

新增或更新您的代碼

AgentSerializer(serializers.py)

class AgentSerializer(serializers.ModelSerializer): 
    class Meta: 
     model = Agents 

    def create(self, validated_data): 
     return Agents.objects.create(**validated_data) 

在views.py

class AgentViewSet(viewsets.ModelViewSet): 
    queryset = Agents.objects.all() 
    serializer_class = AgentSerializer 
    permission_classes = (permissions.AllowAny,) 
    http_method_names = ['post'] 

    def create(self, request): 
     serializer = self.serializer_class(data=request.data) 

     if serializer.is_valid(): 
      Agents.objects.create_user(**serializer.validated_data) 

      return Response(serializer.validated_data, status=status.HTTP_201_CREATED) 

     return JSONResponse(serializer.errors, status=400) 

在models.py:

class MyUserManager(BaseUserManager): 
    def _create_user(self, email, password, 
       is_staff, **extra_fields): 
     password= # Generate random password 
     email = self.normalize_email(email) 
     user = self.model(email=email, 
     is_staff=is_staff, **extra_fields) 
     user.set_password(password) 
     user.save(using=self._db) 
     return user 
    def create_user(self, email, password=None, **extra_fields): 
     return self._create_user(email, password, False 
          **extra_fields) 
+0

關鍵是在AgentViewSet中覆蓋創建方法謝謝 –

0

試試在您的MyUserManager中重寫create_user方法。喜歡的東西:

def create_user(self, **kwargs): 
    kwargs['password'] = # Generate random password 
    return super().create_user(**kwargs) 
+0

這不起作用,因爲create_user永遠不會被restframework –

+0

調用這很奇怪,因爲我有一個使用RF的項目,而且我完全是這樣做的。您是否實施了自定義註冊視圖/方法? – lucasnadalutti