我可以在创建用户之前(或同时)创建依赖于用户的模型吗?

时间:2020-10-02 17:04:07

标签: curl post django-models django-rest-framework

我有以下内容:

MatchIt
class User(AbstractUser):
    ROLES = (
        ('d', 'Doctor'),
        ('s', 'Secretary'),
        ('p', 'Patient'),
    )
    role = models.CharField(
        max_length=1, choices=ROLES, blank=True, default='p', help_text='Role')

class Doctor(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    national_id = models.CharField(max_length=32, default=0, unique=True, blank=False)

我正尝试直接创建一个Doctor,希望我可以同时创建一个User:

class UserSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ('id', 'username', 'password', 'role', 'first_name', 'last_name', 'email', 'phone')
        extra_kwargs = {'password': {'write_only': True, 'required': True}}

class DoctorSerializer(serializers.ModelSerializer):
    class Meta:
        model = Doctor
        fields = ('__all__')

但是,我回来了,我相信这是因为我没有直接创建用户吗?

curl -X POST -H "Content-type: application/json" -d '{"user": {"username": "john", "password": "123456"}}' http://localhost:8000/api/doctor/

0 个答案:

没有答案