Based on the DRF documentation I have a created a list of email_id stored in my model in the following way
Models.py
class UserData(models.Model):
emails = models.CharField(max_length=100,blank=False)
In my serializers.py
file
class UserSerializer(serializers.ModelSerializer):
emails = serializers.ListField(child = serializers.EmailField())
While posting the data, the drf page shows the data in the expected format, i.e
"emails": [
"bal@bal.com"
],
But, if I query the same data using python or any rest client. I get the data in the following format
data = json.load(urllib2.urlopen("http://localhost:8000/blah/id"))
In [46]: d['emails']
Out[46]:
[u'[',
u'u',
u"'",
u'b',
u'a',
u'l',
u'@',
u'b',
u'a',
u'l',
u'.',
u'c',
u'o',
u'm',
u"'",
u']']
Ideally, it should have been
d['emails'] = ['bal@bal.com']
I am not sure, what exactly is wrong in here. Any suggestions ?
Your model only has one email field. It does not support storing multiple email in the database. What you need is something like this:
However, this will result in a slightly different data structure:
If you don't want this data structure, you could create a custom field
Or... if you're using
postgresql
you should be able to do this: