Django Rest Framework-获取相关的FK对象以在模板中使用;POST现在不工作



我已经准备好了一个表单,并使用Vue前端和DRF后端。这是一个用于添加(创建(新模型的表单,并且有一个相关模型的下拉列表,这些模型是正在创建的模型的FK。

我需要访问选定FK项目的属性。

我的序列化程序如下所示:

class SubdomainSerializer(serializers.ModelSerializer):
class Meta:
model = Subdomain
fields = [
"id",
"domain",
"short_description",
"long_description",
"character_code",
]
# def get_absolute_url(self, obj):
#     return obj.get_absolute_url()

class EvidenceSerializer(serializers.ModelSerializer):
created_by = serializers.HiddenField(
default=serializers.CurrentUserDefault()
)
updated_by = serializers.HiddenField(
default=serializers.CurrentUserDefault()
)
absolute_url = serializers.SerializerMethodField()
created_by_name = serializers.SerializerMethodField()
updated_by_name = serializers.SerializerMethodField()
class Meta:
model = Evidence
fields = "__all__"

表单将创建一个新的"证据"项,"子域"是表单上的下拉列表,包含所有相关子域。

型号如下:

class Subdomain(CreateUpdateMixin):
domain = models.ForeignKey(Domain, on_delete=models.PROTECT)
short_description = models.CharField(max_length=100)
long_description = models.CharField(max_length=250)
character_code = models.CharField(max_length=5)
class Evidence(CreateUpdateMixin, CreateUpdateUserMixin, SoftDeletionModel):
subdomain = models.ForeignKey(Subdomain, on_delete=models.PROTECT)
evaluation = models.ForeignKey(
Evaluation, related_name="evidences", on_delete=models.PROTECT
)
published = models.BooleanField(default=False)
comments = models.CharField(max_length=500)

在我的表单中,当用户从下拉列表中选择每个subdomain时,我只想包括它的short_description——我可能也想使用long_description

这是表单中我呈现下拉列表的部分:

<div class="form-group col-sm-4">
<label class="" for="subdomain">Subdomain</label>
<select name="subdomain" id="subdomain" class="form-control" v-model="element.subdomain">
<option v-for="choice in subdomains" :value="choice.id" >{{ choice.character_code }}</option>
</select>
</div>
<div class="small" v-if="element.subdomain">
<!-- THIS IS WHERE I WOULD LIKE TO DISPLAY THE SHORT DESCRIPTION FOR THE CHOICE IN THE DROPDOWN -->
{{ choice.short_description }}
</div>

当我POST:时,表单数据如下所示

evaluation: 2037
subdomain: 448
comments: Test comments to add to the subdomain
published: true
csrfmiddlewaretoken: 382796ryfuasiodfgyhakljyht37yaisdfaslk3r

我尝试过的东西-其中一些是为了显示目的,但似乎破坏了表单/POST:

  • depth=1添加到EvidenceSerializer的Meta中,这起到了作用,但使表单不再适当提交。我想这是因为它想要整个子域而不仅仅是ID?我无法使它工作-子域总是抛出一个错误。

  • 将以下内容添加到我的EvidenceSerializer中,这似乎再次破坏了POST操作,这将导致子域下拉列表引发错误。

subdomain = SubdomainSerializer(read_only=True)

使用上面的这两种方法,下拉列表都无法识别所选的subdomain_id,并且最终都在幕后抛出了这个错误:

Cannot insert the value NULL into column 'subdomain_id', table 'local_app.dbo.myapp_evidence'; column does not allow nulls. INSERT fails.

任何关于如何进行的建议都将是非常棒的。

TLDR;需要能够使用DRF访问下拉列表的FK关系上的属性,并能够以表单形式提交该项目。

感谢@bdbd为我指明了正确的方向。

对于任何好奇的人来说,我使用这些链接解决了这个问题——事实证明,我需要稍微更改一下我的序列化程序:

class SubdomainSerializer(serializers.ModelSerializer):
class Meta:
model = Subdomain
fields = [
"id",
"domain",
"short_description",
"long_description",
"character_code",
]

class EvidenceSerializer(serializers.ModelSerializer):
created_by = serializers.HiddenField(
default=serializers.CurrentUserDefault()
)
updated_by = serializers.HiddenField(
default=serializers.CurrentUserDefault()
)
absolute_url = serializers.SerializerMethodField()
created_by_name = serializers.SerializerMethodField()
updated_by_name = serializers.SerializerMethodField()
# add the 'subdomain' as read only - but with all the attributes
subdomain = SubdomainSerializer(read_only=True)
# add the 'subdomain_id' as a PrimaryKeyRelatedField with the source being the subdomain
subdomain_id = serializers.PrimaryKeyRelatedField(
queryset=Subdomain.objects.all(), source="subdomain"
)
class Meta:
model = Evidence
fields = "__all__"

然后我更新了一点HTML:

<div class="form-group col-sm-4">
<label class="" for="subdomain_id">Subdomain</label>
<select name="subdomain_id" id="subdomain" class="form-control" v-model="element.subdomain">
<option v-for="choice in subdomains" :value="choice" >{{ choice.character_code }}</option>
</select>
</div>
<div class="small" v-if="element.subdomain_id">
{{ element.subdomain.short_description }}
</div>

然后在ajax调用中,我只需将subdomain_id分配给subdomain.id

data: {
evaluation : evaluationId,
subdomain_id : vm.element.subdomain.id,
comments : vm.element.comments,
published: vm.element.published,
csrfmiddlewaretoken: vm.sharedStore.state.csrftoken,
},

相关内容

  • 没有找到相关文章

最新更新