0

新しいユーザーを作成するたびに、対応するユーザー BaseUserが作成されません。department属性なしで チェックしたので、信号は正しく機能しています 。user_typeどうすれば登録でき BaseUserますか?

# models.py
from django.db import models
from django.conf import settings
from django.contrib.auth.models import User


class BaseUser(models.Model):
   # TODO: Add a class with static method to return the list of departments(choices)
   # TODO: (REMINDER) To add placeholder that username is rollnumber

USER_TYPES = (
        (0, 'Student'),
        (1, 'Professor'),
        (2, 'Guest'),
)
user = models.OneToOneField(User, primary_key=True,
        related_name='user')
department = models.CharField(max_length=3, default='CSE')
user_type = models.IntegerField(choices=USER_TYPES, default=0)

REQUIRED_FIELDS = [
        'department',
        'email',
        'first_name',
        'user_type'
]

def get_user_type(self):
    return self.USER_TYPES[self.user_type][1]

def __unicode__(self):
    return str(
            dict((
                self.user.user_name,
                self.get_user_type(),
                ))
            )

# serializers.py
from django.contrib.auth.models import User

from rest_framework import serializers

from authentication.models import BaseUser


class UserSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = (
            'id',
            'username',
            'first_name',
            'email'
        )

class BaseUserSerializer(serializers.ModelSerializer):
    user = UserSerializer()
    user_type = serializers.SerializerMethodField()

    class Meta:
        model = BaseUser
        fields = (
            'user',
            'user_type',
            'department',
        )

    def get_user_type(self, obj):
        return obj.get_user_type()

# views.py
from django.shortcuts import render
from django.contrib.auth.models import User

from rest_framework import viewsets
from rest_framework.decorators import detail_route, list_route

from authentication.models import BaseUser
from authentication.serializers import BaseUserSerializer, UserSerializer


class UserViewSet(viewsets.ModelViewSet):
    queryset = User.objects.all()
    serializer_class = UserSerializer

編集:このアプローチに従おうとしましたが、ユーザーにそれらの属性がないというエラーが発生しました(つまり'Some fields are empty!'

# signals.py
# I have another signal in signals.py and it is working fine.
@receiver(post_save, sender=User)
def create_user_handler(sender, instance, created, **kwargs):
    if created:
        attrs_needed = ['department', 'user_type']
        if all(hasattr(instance, attr) for attr in attrs_needed):
            base_user = BaseUser(
                    user=instance,
                    department=instance.department,
                    user_type=instance.user_type,
            )
            base_user.save()
        else:
            print "Some fields are empty!"
# views
class UserViewSet(viewsets.ModelViewSet):
    queryset = User.objects.all()
    serializer_class = UserSerializer

    @list_route(methods=['post'])
    def create_baseuser(self, request):
        print request.DATA['username']
        user = User.objects.create_user(
                username=request.DATA['username'],
                password=request.DATA['password'],
                email=request.DATA['email'],
                first_name=request.DATA['first_name']
        )

        user.department = request.DATA['department']
        user.user_type = request.DATA['user_type']

        user.save()
        return Response(data='{"detail":"created"')
4

1 に答える 1