0
私のモデルに関係フィールド(herd/herd_id)
が追加されました。それは文句を言わない私のAnimal
モデルのフィールドとしてAPIに表示され、ここでAnimalSerializer
djangorestframework
DjangoRestFramework 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_id
herd
に列名を変更する