Django custom serializer how to input ID/PK -
i trying make custom serializer work. can't use model serializer since saving data multiple models. here code:
class rushserializer(serializers.serializer): keyword = serializers.integerfield(read_only=true) url = serializers.urlfield() def save(self): keyword = self.validated_data['keyword'] url = self.validated_data['url'] url1.objects.create(keyword=keyword, url=url)
and input data this:
nov = {'url': 'https://domain.com/'} nov['keyword'] = id serializer = rushserializer(data=nov)
it returns me error:
keyerror @ /admin/keywords/kw/add/
'keyword'
here model itself:
class url1(models.model): keyword = models.foreignkey(kw) url = models.urlfield()
what wrong code? tried pretty anything here can't seem make work.
edit: clarify, works if model serializer , this:
class meta: model = url1 fields = ('keyword', 'url')
so problem isn't in input 100% in serializer itself.
- if keyword without read_only error:
valueerror @ /admin/keywords/kw/add/ cannot assign "104": "url1.keyword" must "kw" instance.
Comments
Post a Comment