我已经尝试了很多方法来解决我的问题,但是我无法解决。
我想在提供令牌时提供用户详细信息,但出现此错误:/ api / token / get中的## AssertionError
HyperlinkedIdentityField
在串行器上下文中需要该请求。实例化序列化程序时添加context={'request': request}
。##
在views.py中找到代码
class GetUserInfo(RetrieveUpdateAPIView):
# permission_classes = (IsAuthenticated,)
serializer_class = UserSerializer
def get(self, request, *args, **kwargs):
serializer = self.serializer_class(request.user)
# serializer to handle turning our `User` object into something that
# can be JSONified and sent to the client.
return Response({"user": serializer.data}, status=status.HTTP_200_OK)
这是serializers.py中的代码
lass UserProfileSerializer(serializers.ModelSerializer):
class Meta:
model = UserProfile
fields = ('title', 'dob', 'address', 'country', 'city', 'zip', 'photo','director','manager','collaborator')
class UserSerializer(serializers.HyperlinkedModelSerializer):
profile = UserProfileSerializer(required=True)
class Meta:
model = User
fields = ('url', 'email', 'first_name', 'last_name', 'password', 'profile')
extra_kwargs = {'password': {'write_only': True}}
def create(self, validated_data):
profile_data = validated_data.pop('profile')
password = validated_data.pop('password')
user = User(**validated_data)
user.set_password(password)
user.save()
UserProfile.objects.create(user=user, **profile_data)
return user
def update(self, instance, validated_data):
profile_data = validated_data.pop('profile')
profile = instance.profile
instance.email = validated_data.get('email', instance.email)
instance.save()
profile.title = profile_data.get('title', profile.title)
profile.dob = profile_data.get('dob', profile.dob)
profile.address = profile_data.get('address', profile.address)
profile.country = profile_data.get('country', profile.country)
profile.city = profile_data.get('city', profile.city)
profile.zip = profile_data.get('zip', profile.zip)
profile.photo = profile_data.get('photo', profile.photo)
director = profile_data.get('director', profile.director)
manager = profile_data.get('manager', profile.manager)
collaborator = profile_data.get('collaborator', profile.collaborator)
profile.save()
我的urls.py
urlpatterns = [
...
path('token/get', GetUserInfo.as_view()),
...
]```
In complement, I show my model:
```python
class User(AbstractUser):
username = models.CharField(blank=True, null=True,max_length=50)
email = models.EmailField(_('email address'), unique=True)
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['first_name', 'last_name']
def __str__(self):
return "{0} {1} ({2})".format(self.first_name, self.last_name, self.email)
class Company (models.Model):
name = models.CharField(max_length=70)
def __str__(self):
return "{}".format(self.name)
"""
User's detail
"""
class UserProfile(models.Model):
user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name='profile')
title = models.CharField(max_length=5)
dob = models.DateField()
address = models.CharField(max_length=255)
country = models.CharField(max_length=50)
city = models.CharField(max_length=50)
zip = models.CharField(max_length=5)
photo = models.ImageField(upload_to='uploads', blank=True)
director = models.BooleanField(default=False)
manager = models.BooleanField(default=False)
collaborator = models.BooleanField(default=False)
company = models.ForeignKey(Company, null=True, on_delete=models.CASCADE, related_name='compnay')
您能帮我解决此错误并让我的Json吗?