2017-07-21 11 views
0

私はRESTバックエンドを稼働させようとしています。これは私の最初のPythonです。 インシデントは多くのアンダーウェイを持つことができますが、アンダーウェイはインシデント(ManyToOne)を1つしか持てません。DRF PrimaryKeyRelatedField、AttributeError: 'QuerySetオブジェクトに属性パラメータがありません'

UnderWayモデルのシリアライザを使用しようとしていますが、次のパラメータ['incident'、 'phonenumber'、 'time']を使用してPOSTとGETリクエストを行う必要があります。

私はすべてを試したように感じますが、取得するのはAttributeErrorで、 'QuerySet'オブジェクトには属性 'incident'がありません。

models.py:

class Incident(models.Model): 
active = models.BooleanField(default=False) 
message = models.TextField(max_length=200, blank=True) 
time = models.TimeField(auto_now=False, auto_now_add=False) 
created_at = models.DateTimeField(auto_now=True, auto_created=True) 
updated_at = models.DateTimeField(auto_now=True) 


class UnderWay(models.Model): 
    id = models.BigAutoField(primary_key=True) 
    incident = models.ForeignKey(Incident, null=True) 
    telephone = models.CharField(max_length=30, blank=True) 
    time = models.CharField(max_length=30, blank=True) 
    created_at = models.DateTimeField(auto_now=True, auto_created=True) 

views.py

class LastIncidentApiView(generics.ListCreateAPIView): 
"""This class defines the create behavior of our rest api.""" 
queryset = [Incident.objects.order_by('created_at')[0]] 
serializer_class = IncidentSerializer 

def perform_create(self, serializer): 
    """Save the post data when creating a new incident.""" 
    serializer.save() 


class UnderWayApiView(generics.ListCreateAPIView): 
    """This class defines the create behavior of our rest api.""" 
    # active = Incident.objects.latest('created_at').pk 
    queryset = [UnderWay.objects.all()] 
    serializer_class = UnderWaySerializer 

    def perform_create(self, serializer): 
     """Save the post data when creating a new incident.""" 
     serializer.save() 

serializers.py

class IncidentSerializer(serializers.ModelSerializer): 

class Meta: 
    """Meta class to map serializer's fields with the model fields.""" 
    model = Incident 
    fields = ('id', 'active', 'message', 
       'time', 
       'created_at', 'updated_at') 
    read_only_fields = ('created_at', 'updated_at') 


class UnderWaySerializer(serializers.ModelSerializer): 
    incident = serializers.PrimaryKeyRelatedField(queryset=Incident.objects.all().values_list('pk', flat=True)) 

    class Meta: 
     model = UnderWay 
     fields = ('incident', 'telephone', 'time', 'created_at') 
     read_only_field = 'created_at' 

しかし私はGET-を得るのですかシリアライザ内のインシデントフィールドをReadOnlyFieldにすると動作するように要求します。しかし、それは本当に私を助けてくれていません。任意の助け

感謝:)

+0

DRFが期待するものをより尊重する必要があります:クエリセットをジャンゴされていない、あなたのクエリセットが、リスト。それは問題だとは言えませんが、それは問題です。 –

+0

こんにちは、私は多分あなたがクエリーセットを誤解していると思います。クエリーセットは反復可能なオブジェクトなので、リストに入れる必要はありません。 '[Underway.objects.all()]'と書くと、実際にはクエリーセットである1つの要素を持つリストが作成されます。クエリーセットについての良いチュートリアルがいくつかあります:https://tutorial.djangogirls.org/ja/django_orm/ –

答えて

0

、あなたがIncidentApiViewを定義する必要があります

queryset=Incident.objects.all() 

とperform_create(無駄なIMO、ListCreateAPIViewの作成部分から来るもの)

LastIncidentViewは、最後のものだけを取得するためにperform_readをオーバーライドするListAPIViewである可能性があります。事件は」、あなただけのクエリセットはUnderWaySerializerについては

UnderWay.objects.all() 

incident = serializers.PrimaryKeyRelatedField(queryset=Incident.objects.all()) 
である必要があり、一度に現在の最後になる事件、)UnderWayApiViewについては

を投稿したいです

が、私はそれはあなたの問題を解決することを言っていません

良いはずですが、それはあなたの意見に不良STHがあります

0

変更シリアライザで、このライン、クリーンなコードについては

incident = serializers.PrimaryKeyRelatedField(queryset=Incident.objects.all()) 

そして、あなたのビューで、

queryset = UnderWay.objects.all() 
関連する問題