Я использую Django Rest Framework и попытался добавить настраиваемое поле, которого нет в модели HPIQuestionBank
, в файл serializers.py, и пока код такой, как показано ниже, но я получаю сообщение об ошибке. Я знаю, что checkboxes
и checkboxValues
не являются атрибутами модели, но моя цель состоит в том, чтобы сделать их custom fields
где я ошибаюсь? answer_type
- это настраиваемое поле, оно в порядке и работает правильно.
Got AttributeError when attempting to get a value for field `checkboxes` on serializer `TemplateQuestionBankSerializer`.
The serializer field might be named incorrectly and not match any attribute or key on the `HPIQuestionBank` instance.
Original exception text was: 'HPIQuestionBank' object has no attribute 'checkboxes'.
сериализатору
class TemplateQuestionBankSerializer(serializers.ModelSerializer):
answer_type = serializers.CharField(write_only=True)
checkboxes = serializers.ListField(child=serializers.CharField(write_only=True))
checkboxValues = serializers.ListField(child=serializers.CharField(write_only=True))
class Meta:
model = HPIQuestionBank
fields = ['id','label','answer_type','checkboxes','checkboxValues']
модель
class HPIQuestionBank(models.Model):
label = models.CharField(
max_length=200,
db_index=True,
blank=True,
null=True)
template = models.ForeignKey(
HPIFilter, blank=True, null=True, on_delete=models.CASCADE, default='')
organization = models.IntegerField(blank=True, null=True)