0
django에서 사용자 정의 사용자를 만들고 싶습니다.하지만 많은 문제가 있습니다. 다음은 그 마일 코드 :사용자 Django 휴식 프레임 워크를 확장
models.py
class Profesionales(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
dni = models.CharField(max_length=9)
numColegiado = models.CharField(max_length=8)
valoracionMedia = models.FloatField()
numVotos = models.IntegerField()
def __str__(self):
return self.numColegiado, self.user.first_name
Serializers.py
class ProfesionalesSerializer(serializers.ModelSerializer):
class Meta:
model = Profesionales
fields = ('numColegiado')
class UserSerializer(serializers.ModelSerializer):
profesionales = ProfesionalesSerializer
class Meta:
model = User
fields = (
'id', 'username', 'email', 'first_name', 'last_name', 'password', 'profesionales'
)
def create(self, validated_data):
profesional_data = validated_data.pop('profesionales')
user = User.objects.create(**validated_data)
Profesionales.objects.create(**profesional_data)
return user
views.py
#Listar todos los profesionales o crear uno
#profesionales/
class ProfesionalesList(APIView):
def get(self, request):
profesionales = User.objects.all()
profesionalSerializer = UserSerializer(profesionales, many=True)
return Response(profesionalSerializer.data)
def post(self, request):
profesionalSerializer = UserSerializer(data=request.data)
if profesionalSerializer.is_valid():
profesionalSerializer.save()
return Response(profesionalSerializer.data, status=status.HTTP_201_CREATED)
else:
return Response(profesionalSerializer.errors, status=status.HTTP_400_BAD_REQUEST)
오류! :
OperationalError at /profesionales/
no such column: profesionales_profesionales.user_id
Request Method: GET
Request URL: http://127.0.0.1:8000/profesionales/
Django Version: 1.11.6
마이그레이션을 수행 했습니까? –
예, 물론입니다! :) – Isma9