2016-11-11 5 views
0

私のモデルに関係フィールド(herd/herd_id)が追加されました。それは文句を言わない私のAnimalモデルのフィールドとしてAPIに表示され、ここでAnimalSerializerdjangorestframeworkDjangoRestFramework APIに追加された新しいリレーショナルフィールド

は私の動物モデルとシリアライザです:

models.py

class Animal(models.Model): 

    this_id = models.CharField(max_length=25) 
    name = models.CharField(max_length=25) 
    species_type = models.CharField(max_length=25) 
    breed = models.CharField(max_length=25) 
    date_of_birth = models.DateField() 
    birth_weight = models.IntegerField() 
    sex = models.CharField(max_length=7) 
    sibling_order = models.IntegerField() 
    sire_id = models.CharField(max_length=20) 
    sire_name = models.CharField(max_length=25) 
    dam_id = models.CharField(max_length=25) 
    dam_name = models.CharField(max_length=25) 
    rf_id = models.CharField(max_length=25) 
    comment = models.TextField(max_length=250, null=True) 

    herd = models.ForeignKey(Herd, related_name='animals', on_delete=models.CASCADE) 

    created_at = models.DateTimeField(auto_now_add=True, editable=False) 
    updated_at = models.DateTimeField(auto_now=True, editable=False) 

    class Meta: 
     ordering = ('name',) 

serializers.py

class AnimalSerializer(serializers.ModelSerializer): 

    class Meta: 
     model = Animal 
     fields = (
      'this_id', 
      'name', 
      'species_type', 
      'breed', 
      'date_of_birth', 
      'birth_weight', 
      'sibling_order', 
      'sex', 
      'sire_id', 
      'sire_name', 
      'dam_id', 
      'dam_name', 
      'rf_id', 
      'comment', 
      'herd_id', // <--- this field wont appear in the djangorestframework UI. 
      'created_at', 
      'updated_at', 
     ) 
     read_only_fields = ('id', 'created_at', 'updated_at') 

ここに画像があります。表示されないherd_idフィールドを探します。 Djangoは、一致するシリアル化の名前を認識しますので、私はちょうどherd_idherdに列名を変更する

djangorestframework

答えて

0

を助けてください。

関連する問題