我有用户设置模型:
class UserSettings(models.Model):
user = models.ForeignKey('userprofile.UserProfile' related_name='own_user')
lookup_user = models.ManyToManyField('userprofile.UserProfile')
我写了一个自定义字段:
class SettingKeywordsField(serializers.RelatedField):
def to_native(self, value):
eturn { str(value.pk): value.username }
并在序列化器中使用:
class SettingsSerializer(serializers.ModelSerializer):
lookup_user = SettingKeywordsField(many=True)
class Meta:
model = UserSettings
fields = ('lookup_user')
在这种情况下,lookup_user不会出现在html表单选项卡中。
我的序列化程序出了什么问题? 提前致谢
答案 0 :(得分:1)
serializers.RelatedField
默认为read_only=True
...尝试:
class SettingsSerializer(serializers.ModelSerializer):
lookup_user = SettingKeywordsField(many=True, read_only=False)
class Meta:
model = UserSettings
fields = ('lookup_user')