2017-08-15 26 views
1

試圖獲得DRF項目中的基本消息傳遞功能。我似乎有嵌套的序列化器驗證問題。我是DRF的新手,已經閱讀了幾天的文檔,但我一定錯過了一些東西。由於「無法獲取QuerySet類的repr」錯誤導致嵌套序列化程序的Null約束失敗

message = Message.objects.create(**validated_data)內發生在我serializer.The請求錯誤返回Integrity error, NOT NULL constraint failed on accounts_message.sender_id,而是通過調試我發現,這是一個錯誤的query.py在QuerySet的方法造成的:self: unable to get repr for <class 'django.db.models.query.QuerySet'> model: <class 'accounts.models.Message'> query: none using: none

這似乎成爲導致我的問題的原因,因爲序列化程序生成的字典似乎具有在請求中傳遞的所有數據,包括假定的null sender_id。但是,我不知道如何解決這個問題。我需要覆蓋repr嗎?或查詢集?這感覺就像我堅持相對簡單的東西。

Serializers.py

class UserSerializer(serializers.ModelSerializer): 
    class Meta: 
     model = User 
     fields = ('id', 'name', 'email') 
     extra_kwargs = { 
      'id': { 
       'validators': [UnicodeUsernameValidator()], 
      } 
     } 

class MessageSerializer(serializers.ModelSerializer): 
    sender = UserSerializer() 
    recipients = UserSerializer(many=True) 


    class Meta: 
     model = Message 
     fields = ('id', 'subject', 'body', 'read', 'sender', 'recipients') 


    def create(self, validated_data): 
     sender_data = validated_data.pop('sender') 
     recipient_data = validated_data.pop('recipients') 
     message = Message.objects.create(**validated_data) 

     for user in recipient_data: 
      user= User.message_recip.get_or_create(id=user['id'], name=user['name']) 
      message.recipients.add(user) 

     for user in sender_data: 
      user= User.objects.get_or_create(id=user['id'], name=user['name']) 
      message.sender.add(user) 
     return message 

Models.py

from django.db import models 

class User(models.Model): 
    id = models.IntegerField(primary_key=True) 
    name = models.CharField(max_length=30) 
    email = models.CharField(max_length=75) 

    def __str__(self): 
     return self.name 

class Message(models.Model): 
    id = models.IntegerField(primary_key=True) 
    subject = models.CharField(max_length=250) 
    body = models.CharField(max_length=5000) 
    read = models.BooleanField(default=False) 
    sender = models.ForeignKey(User, related_name='message_sender') 

    recipients = models.ForeignKey(User, related_name='message_recip', default=1) 

Views.py

class MessageList(APIView): 

    def get(self, request): 
     messages = Message.objects.all() 
     serializer = MessageSerializer(messages, many=True) 
     return Response(serializer.data) 

    def post(self, request): 
     serializer = MessageSerializer(data=request.data) 
     if serializer.is_valid(): 
      serializer.save() 
      return Response(serializer.data, status=status.HTTP_201_CREATED) 
     return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) 


class User(APIView): 

    def get(self, request): 
     user = User.objects.get(id) 
     serializer = UserSerializer(user, many=False) 
     return Response(serializer.data) 

回答

0

如果你給null=Trueblank=True選項對您的ForeignKeys,您不會再得到IntegrityError。由於您沒有添加空&空白選項,Django ORM在您的數據庫中創建了一個約束,並且不會讓您向外鍵添加空值。 Django Model Field Reference

+0

不錯,這有助於它的一部分,儘管QuerySet錯誤似乎仍然是問題的根源。我仍然無法找到查詢集錯誤的repr,但它的User對象,而不是消息對象。我看看模型領域的參考... –

相關問題