Django Rest Framework,对url / group1 / messages的POST请求

时间:2019-07-09 17:14:32

标签: django url post django-rest-framework django-views

我想在django rest框架中创建的聊天室中添加一条消息。我的问题是没有页面被调用:group1 / messages。但是,如果我进入url / group,我可以看到不同级别的所有消息。如果我输入url / messages,则会出现所有组的消息!

如何进行设置,以便对url / group1 / messages发出发布请求?

Django模型:

class UserProfile(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)

class Meta:
     verbose_name_plural = 'All Users'

def __str__(self):
     return self.user.username

@receiver(post_save, sender=User)
def create_user_data(sender, update_fields, created, instance, **kwargs):
if created:
    user = instance
    profile = UserProfile.objects.create(user=user)

class Message(models.Model):
    sender = models.ForeignKey(UserProfile, on_delete=models.CASCADE,  related_name="sendermessage")
    content = models.CharField(max_length=500)
    date = models.DateField(default=date.today)
    canview = models.ManyToManyField(UserProfile, blank=True, related_name="messagecanview")

class Meta:
    verbose_name_plural = 'Messages'

def __str__(self):
    return "{sender}".format(sender=self.sender)

class Room(models.Model):
    name = models.CharField(max_length=50)
    members = models.ManyToManyField(UserProfile, blank=True)
    messages = models.ManyToManyField(Message, blank=True)

class Meta:
    verbose_name_plural = 'Rooms'

def __str__(self):
    return "{name}".format(name=self.name)enter code here

Django序列化器:

class UserProfileSerializer(serializers.ModelSerializer):
    username = serializers.CharField(source='user.username')
    class Meta:
        model = UserProfile
        fields = ('id', 'username')

class MessageSerializer(serializers.ModelSerializer):
    sender = UserProfileSerializer()
    class Meta:
        model = Message
        fields = ('id', 'content', 'date', 'sender')

class RoomSerializer(serializers.ModelSerializer):
    messages = MessageSerializer(many=True)
    members = UserProfileSerializer(many=True)
    class Meta:
        model = Room
        fields = ('id', 'name', 'members', 'messages')

Django视图:

class UserProfileView(viewsets.ModelViewSet):
    http_method_names = ['get', 'post', 'put', 'delete', 'patch']
    queryset = UserProfile.objects.all()
    serializer_class = UserProfileSerializer

class MessageView(viewsets.ModelViewSet):
    http_method_names = ['get', 'post', 'put', 'delete', 'patch']
    queryset = Message.objects.all()
    serializer_class = MessageSerializer

class UserMessageView(MessageView):
def get_queryset(self):
    return Message.objects.filter(canview__user=self.request.user)

class RoomView(viewsets.ModelViewSet):
    http_method_names = ['get', 'post', 'put', 'delete', 'patch']
    queryset = Room.objects.all()
    serializer_class = RoomSerializer

class UserRoomView(RoomView):
def get_queryset(self):
    return Room.objects.filter(members__user=self.request.user)

Django Urls:

 router = routers.DefaultRouter()
 router.register('users', views.UserProfileView),
 router.register('rooms', views.UserRoomView),
 router.register('messages', views.UserMessageView),

urlpatterns = [
    path('', include(router.urls)),
]

0 个答案:

没有答案