什么是serializers.StringRelatedField的读/写等效项? [英] What is the read/write equivalent of serializers.StringRelatedField?

查看:81
本文介绍了什么是serializers.StringRelatedField的读/写等效项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Django Rest Framework 3中,我想返回pk关系的unicode值,这是可以使用serializer.StringRelatedField的方式,但是我也需要该值可写.StringRelatedField是只读的.

In Django Rest Framework 3, I want to return the unicode value of a pk relationship, the way you can using a serializer.StringRelatedField, but I need the value to be writable, too. StringRelatedField is read only.

我不在乎API是否在PUT上接受pk或字符串值(尽管接受字符串会很漂亮,并且可以节省我所有的pk!).该API仅需要在GET上返回unicode字符串值.

I don't care if the API accepts the pk, or the string value, on the PUT (though accepting the string would be nifty, and would save me grabbing all the pks!). The API just needs to return the unicode string value on the GET.

我正在考虑使用PrimaryKeyRelatedField,但是查询是什么样的?

I'm thinking PrimaryKeyRelatedField might be the way to go, but what does the query look like?

例如,如果我想要的模型是模型",并且我想要Model.name进行序列化,那么该命令是什么样的:

For instance, if the model I want is "Model", and I want Model.name to be serialized, what does this command look like:

name = serializers.PrimaryKeyRelatedField(queryset=Model.objects.get(pk=????))

我正在努力,因为我不知道如何从序列化器对象中获取pk以便查询相关模型...

I'm struggling because I don't know how to get the pk from the serializer object in order to query the related model ...

那当然是我需要的是PrimaryKeyRelatedField.这可能是完全错误的.

That's presuming PrimaryKeyRelatedField is what I need, of course. Which may be totally wrong.

谢谢

约翰

以下是根据要求提供的示例模型,为清楚起见,略有更改:

Here are example models as requested, slightly changed for clarity:

class CarModel(models.Model):
  name = models.CharField(max_length=100,unique=True)

  def __str__(self):
    return self.name


class Car(models.Model):
  name = models.CharField(max_length=100)
  make = models.ForeignKey(CarMake)
  car_model = models.ForeignKey(CarModel)

class CarSerializer(serializers.ModelSerializer):
   car_model = serializers.StringRelatedField() //like this, but read/write

   class Meta:
   model = Car

在此示例中,我对Car进行序列化,尝试返回CarModel的字符串值,该字符串值可以使用表单中的select下拉列表进行更新.如果我使用不同的序列化器,一个用于POST期望PK,另一种用于返回字符串的其他序列化器,则表格中的select指令将变得非常混乱.

In this example I'm serializing Car, trying to return a string value of CarModel that can be updated with a select dropdown in a form. If I use different serializers, one for POST that expects the PK and one for everything else that returns the string, the select directive in the form gets very messy.

因此,理想情况下,我只希望能够发布字符串值,并让API完成查找并将字符串另存为PK.

So, ideally, I just want to be able to POST the string value, and have the API complete the lookup and save the string as a PK.

推荐答案

我只希望能够发布字符串值,并让API完成查找并将字符串另存为PK."

"I just want to be able to POST the string value, and have the API complete the lookup and save the string as a PK."

这意味着名称"应该是唯一的.如果不是唯一的,则查找可能会返回多个实例.在示例中,您当前使用的名称"不是唯一的,但是如果是,则可以使用...

That would imply that 'name' should be unique. If it isn't unique then the lookup might return several instances. In the example you currently have 'name' isn't unique, but if it was then you could use...

car_model = serializers.SlugRelatedField(queryset=..., lookup_field='name')

我不确定这是否是您真正想要的.澄清这类问题的最佳方法通常是暂时忘掉代码,而只专注于精确描述所需的输入和输出表示形式?...

I'm not convinced if that's actually what you want though. The best way to clarify these sorts of questions is typically to forget about the code for a moment, and just focus on a precise description of what you want the input and output representations to look like?...

这篇关于什么是serializers.StringRelatedField的读/写等效项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆