I’m trying to save data from a facebook graph query, which returns location info in the format:
friend: {
location: {
id: "106078429431815",
name: "London, United Kingdom"
},
}
Or the location key is missing entirely if the user hasn’t set that variable, or it’s set to ‘null’.
The location field in the Model is defined as:
location = models.CharField(max_length=255, blank=True, null=True)
I’m storing the data in the model using the following code, which caters for both the initial query to create the record as well as updates just in case values change in the future:
friend = get_facebook_friend(friend_id=friend_id)
try:
location_name = data['location'].get('name', '')
except:
location_name = ''
if friend:
friend.name = data['name']
friend.location = location_name,
friend.full_clean()
friend.save()
else:
friend = Friend(
name=data['name'],
location=location_name)
friend.full_clean()
friend.save()
This seems to work pretty well for the first insert, which stores values for the Location column in the database as:
- 'London, United Kingdom'
- ''
- NULL
However on subsequent updates, this is storing strange results in the database table, for instance:
(u'London, United Kingdom',)
('',)
(None,)
This same behavior is not observed in other CharFields storing strings from Facebook queries
I’m quite confused D:, Help!
due to the tailing comma:
assigns the tuple
(location_name, )(the brackets are optional)to
friend.location