Skip to content Skip to sidebar Skip to footer

Posting A List Using Django Rest Frameworks

New to using DRF. Curious as to what approaches some of you have used regarding storing a List.... I tried to implement the newly added ListField but I was not able to get it work

Solution 1:

I use this

deep_link = serializers.ListSerializer(child=serializers.CharField())

it's basically a shortcut of yours. The problem with your code is that you are confusing Models with Serializers. The Model is the database representation of your data. The model does not specify serializers. The serializer is the serialized representation of your model coming from the DB, used mostly for data transfer.

classSomeClass(models.Model):
    somenames = StringListFieldsomekey=  models.CharField(max_length=140, default='username')

here, you are telling your model that your Django model's field "somenames" is of type serializers.ListSerializers. That is not what you want. If you want to link a model to a list of strings you need to create a ManyToMany relationship or whatever you need. Below an example

className(models.Model):
    name = models.CharField(max_length=100)

classSomeClass(models.Model):
    somekey = models.CharField(max_length=100)
    somenames = models.ManyToManyField(Names)

Then in your serializers you will have

classNameSerializer(serializers.ModelSerializer):
    name = serializers.CharField()

classSomeClassSerializer(serializers.ModelSerializer):
    somenames =NameSerializer(many=True)
    somekey = serializers.CharField()

    class Meta:
        model = SomeClass

Solution 2:

Ok so after delving into the DRF documentation, along with the help of @xbirkettx answer, I was able to achieve the result I wanted:

in models.py Create your models. I set a ForeignKey relationship to the parent class (in this case the Mention class)

classMention(models.Model):
     ...


classHashtag(models.Model):
    mention = models.ForeignKey(Mention, related_name='hashtags')
    tagname = models.CharField(max_length=100)

    classMeta:
        unique_together = ('mention', 'tagname')

    def__unicode__(self):
        return'%s' % (self.tagname)

in serializers.py Create the serializers for the item in the List (Hashtag) for read/write you need to define to_internal_value method

classHashListingField(serializers.RelatedField):  defto_internal_value(self, data):
        tagname = data
        return {
            'tagname': tagname
        }
    defto_representation(self, value):
        return value.tagname

be sure to add the queryset argument to the serializer for write access

classMentionSerializer(serializers.ModelSerializer):
    hashtags = HashListingFieldSerializer(many=True, queryset=Hashtag.objects.all())
    classMeta:
        model = Mention


    defcreate(self, validated_data):
        tags_data = validated_data.pop('hashtags')
        mention = Mention.objects.create(**validated_data)
        for tags_data in tags_data:
            Hashtag.objects.create(mention=mention, **tags_data)
        return mention

Post a Comment for "Posting A List Using Django Rest Frameworks"