何时在“我的设备”中通过身份验证的用户发帖,我必须检查该设备是否与此用户关联的ID?为此,我已经使用request.user获取了所有信息,但是我只是获得了电子邮件地址,对此有什么解决方案吗?
class User(AbstractUser):
"""User model."""
username = None
email = models.EmailField(_('email address'), unique=True)
mobile_token = models.CharField(max_length=20,blank=True,null=True)
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = []
objects = UserManager_1()
class Device(models.Model):
# user_id = models.CharField(max_length=20,primary)
device_id = models.CharField(max_length=20 ,primary_key=True )
email = models.ForeignKey(User,on_delete=models.CASCADE,blank = True,null=True)
date_added = models.DateField(default=timezone.now)
def __str__(self):
return self.device_id
class UserSerializer(serializers.ModelSerializer):
# my_field = serializers.SerializerMethodField()
# def get_field_name(self, obj):
# return "name"
class Meta:
model = models.User
# key= {'status':'success'}
fields = ('first_name', 'email','last_name','mobile_token',)
class DeviceSerializer(serializers.ModelSerializer):
# locations = serializers.PrimaryKeyRelatedField(many=True,
queryset=models.User.objects.all())
class Meta:
model = models.Device
fields = ('date_added','email','device_id',)
class DeviceView(viewsets.ViewSet):
queryset = models.Device.objects.all()
serializer_class = serializers.DeviceSerializer
def list(self,request):
queryset = models.Device.objects.all()
serializer = serializers.DeviceSerializer(queryset, many=True)
final_data = serializer.data
import json
api={}
api['data'] = final_data
api['message'] = "Device ids"
# Device_name_api = final_data[]
# validators = serializer.get_validators()
api['status'] = 1
# print(request.data)
return Response(api)
def create(self, request, *args, **kwargs):
# response = super().post(request, *args, **kwargs)
# print(self.response)
serializer = self.serializer_class(data=request.data)
print(request.user)
# user_data=models.User.objects.all()
# print(user_data)
# print(request.META['HTTP_X_MYHEADER'])
# print(request.data['device_id'])
if serializer.is_valid():
return Response({
'data':serializer.data,
'status': 1,
'message': 'Succuess'
}, status=status.HTTP_400_BAD_REQUEST)
答案 0 :(得分:1)
尝试使用depth
选项:
class DeviceSerializer(serializers.ModelSerializer):
class Meta:
model = models.Device
fields = ('date_added','email','device_id',)
depth = 1