Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

how to post data to foreign key in django rest framework

class ProfileSerializer(serializers.ModelSerializer):
    class Meta:
        model=Profile
        fields=('id','user','email','birthday','gender','bio','points')
        read_only_fields = ('created','updated')

    def to_representation(self, instance):
        self.fields['user'] =  UserSerializer(read_only=True)
        return super(ProfileSerializer, self).to_representation(instance)
Comment

how to post data to foreign key in django rest framework

class ArticleSerializer(serializers.ModelSerializer):
    tags = serializers.CharField()

    class Meta:
        model = Article
        fields = '__all__'

    def create(self, validated_data):
        tag = validated_data.pop('tags')
        tag_instance, created = Tag.objects.get_or_create(name=tag)
        article_instance = Article.objects.create(**validated_data, tags=tag_instance)
        return article_instance
Comment

how to post data to foreign key in django rest framework

class ContentSerializer(serializers.ModelSerializer):
    class Meta:
        model = Content
        fields = ('title', 'body', 'topic')

    def to_representation(self, instance):
        response = super().to_representation(instance)
        response['topic'] = TopicSerializer(instance.topic).data
        return response
Comment

PREVIOUS NEXT
Code Example
Python :: python keyboard input arrow keys 
Python :: python check if string or list 
Python :: python breadth first search 
Python :: head first python 
Python :: lambda function if else in python 
Python :: sort lexo python 
Python :: abstarct class python 
Python :: binary search recursive python 
Python :: python dataframe appendisnt showing 
Python :: for loops python 
Python :: concatenating strings in python 
Python :: python print every character in list as string 
Python :: python type annotations list of specific values 
Python :: python iterate over string 
Python :: http python lib 
Python :: get type name python 
Python :: knuth morris pratt algorithm 
Python :: dict to tuple 
Python :: python read from stdin pipe 
Python :: django test imagefield 
Python :: writing to a file, with echo 
Python :: anaconda python 3.6 download 
Python :: pickle python 
Python :: sklean tfidf 
Python :: displace items in array python 
Python :: login page in python flask with database 
Python :: numpy combine two arrays selecting min 
Python :: pygame moving shape 
Python :: how to add items to tuple in python 
Python :: mosaicplot pandas 
ADD CONTENT
Topic
Content
Source link
Name
2+1 =