django - “响应”对象没有属性“用户”
问题描述
我收到错误 AttributeError: 'Response' object has no attribute 'user' 对于我编写的以下代码
我正在尝试从上下文中获取用户信息并创建一个通知模型。返回语句时出现上述错误。我不明白为什么我会收到此错误
模型
class CourseNotification(models.Model):
uid = models.UUIDField(
primary_key=True,
default=uuid.uuid4,
editable=False,
unique=True)
course = models.ForeignKey('Course.Course', on_delete=models.SET_NULL, null=True)
user = models.ManyToManyField('Profile.myUser',null=True)
def get_user(self):
return [i for i in self.user.all()]
def __str__(self):
return self.course.course_title
看法
class CourseNotificationView(ModelViewSet):
queryset = CourseNotification.objects.all()
serializer_class = CourseNotificationSerializer
authentication_classes = [JWTAuthentication]
permission_classes = [IsAuthenticated]
def get_queryset(self):
if self.request.user.email is not None:
profile = myUser.objects.get(email=self.request.user.email)
if profile is not None:
notification = CourseNotification.objects.filter(user=profile)
return notification
else:
return Response(data={"User": "Unauthorized User"}, status=HTTP_401_UNAUTHORIZED)
def retrieve(self, request, *args, **kwargs):
serializer = self.get_serializer(self.get_queryset(), many=True)
return Response(data=serializer.data)
串行器
class CourseNotificationSerializer(serializers.ModelSerializer):
class Meta:
model = CourseNotification
fields = '__all__'
def create(self, validated_data):
users = self.context['request'].user
subject = validated_data['course']
if users is None and subject is None or subject == "":
raise serializers.ValidationError({"Invalid": "Subject could not be Invalid"})
checkNotification = self.checkNotification(users, subject)
if checkNotification is not None and checkNotification.status_code == 200:
return checkNotification
validate_subject = self.validateSubject(users, subject)
if validate_subject.status_code == 200:
return validate_subject
get_data = CourseNotification.objects.create(course=subject)
get_data.user.add(users)
get_data.save()
return Response(data=get_data, status=HTTP_201_CREATED, content_type="application/json")
@staticmethod
def checkNotification(users, subject):
get_data = CourseNotification.objects.filter(user=users, course=subject)
if get_data:
for data in get_data:
data.user.remove(users)
data.save()
return Response(data=get_data, status=HTTP_200_OK, content_type="application/json")
@staticmethod
def validateSubject(users, subject):
get_data = CourseNotification.objects.filter(course=subject).exclude(user=users)
if get_data:
subject = CourseNotification.objects.get(course=subject)
subject.user.add(users)
subject.save()
return Response(data=get_data, status=HTTP_200_OK, content_type="application/json")
我正在尝试通过 API 将数据添加到模型中。我正面临问题
解决方案
推荐阅读
- postgresql - 不能在 DBunit 中使用外部表
- java - ArrayList 的排列
- r - 从大空间像素数据帧计算面积
- intellij-idea - IntelliJ - 在文本“FirstSecondThirdFourth”中选择一个单词
- datatables - 搜索或排序时数据表冻结
- javascript - 相同元素上的触摸和单击事件以及奇怪的特定于浏览器的(?)问题
- powershell - Powershell 作业执行可以正确处理批处理,而无需等待整个批处理完成
- c# - IHostedService - 持久任务
- c - C - char *' 与 'char (*)[200] 的间接级别不同
- perl - NPCD:错误:执行的命令退出并返回代码“255”