序列化方法错误

时间:2018-06-28 11:16:59

标签: django serialization django-rest-framework

你好,我有品牌模型和品牌编辑器模型,它们被用作面具来收集公司的所有个人品牌。我想将Brand添加到用户的BrandEditor中,这将创建品牌实例,如果没有BrandEditor,请先创建它。输入验证码:

class BrandAddSerializer(serializers.ModelSerializer):
    editor = serializers.SerializerMethodField('create_editor')

    class Meta:
        model = Brand
        fields = ('id', 'editor', 'name', 'image', 'description')

    def create_editor(self):
        if BrandEditor.objects.filter(owner__user=CurrentUserDefault).exists():
            editor = BrandEditor.objects.filter(owner__user=CurrentUserDefault)
            return editor
        else:
            company = Company.objects.filter(user=CurrentUserDefault)
            BrandEditor.objects.create(owner=company)
            editor = BrandEditor.objects.filter(owner__user=CurrentUserDefault)
            return editor

我抓到create_editor() takes 1 positional argument but 2 were given,我在做什么错?谢谢!

1 个答案:

答案 0 :(得分:2)

绑定到const http = require('http'); const EventEmitter = require('events'); const kafka = require('kafka-node'); const uuidv4 = require('uuid/v4'); const kafkaProduceTopic = "req-res-topic"; const kafkaConsumeTopic = "req-res-topic"; class ResponseEventEmitter extends EventEmitter {} const responseEventEmitter = new ResponseEventEmitter(); var HighLevelProducer = kafka.HighLevelProducer, client = new kafka.Client(), producer = new HighLevelProducer(client); var HighLevelConsumer = kafka.HighLevelConsumer, client = new kafka.Client(), consumer = new HighLevelConsumer( client, [ { topic: kafkaConsumeTopic } ], { groupId: 'my-group' } ); var s = http.createServer(function (req, res) { // Generate a random UUID to be used as the request id that // that is used to correlated request/response requests. // The internal micro-services need to include this id in // the "final" message that is pushed to Kafka and consumed // by the ui service var id = uuidv4(); // Send the request data to the internal back-end through Kafka // In real code the Kafka message would be a JSON/protobuf/... // message, but it needs to include the UUID generated by this // function payloads = [ { topic: kafkaProduceTopic, messages: id}, ]; producer.send(payloads, function (err, data) { if(err != null) { console.log("Error: ", err); return; } }); responseEventEmitter.once(id, () => { console.log("Got the response event for ", id); res.write("Order " + id + " has been processed\n"); res.end(); }) }); s.timeout = 10000; s.listen(8080); // Listen to the Kafka topic that streams messages // indicating that the request has been processed and // emit an event to the request handler so it can finish. // In this example the consumed Kafka message is simply // the UUID of the request that has been processed (which // is also the event name that the response handler is // listening to). // // In real code the Kafka message would be a JSON/protobuf/... message // which needs to contain the UUID the request handler generated. // This Kafka consumer would then have to deserialize the incoming // message and get the UUID from it. consumer.on('message', function (message) { responseEventEmitter.emit(message.value); }); 的方法将object作为第二个参数。因此,您需要这样定义它:

SerializerMethodField

UPD

def create_editor(self, obj): 也是用于向序列化器字段提供默认值的类。要让当前用户使用方法,可以改用此方法(文档related part):

CurrentUserDefault