Gender is not a member of the user table? - facebook-graph-api

I am using following query to find gender of app user.
q=select gender from user where uid=user_id
but the output is
"error": {
"message": "(#602) gender is not a member of the user table.",
"type": "OAuthException",
"code": 602
}
I have seen gender in user table on facebook api,
where is the problem??

Gender is represented as "sex" column in users table.
you can do like this : q = select sex from user where uid=user_id.
For more reference check this : http://developers.facebook.com/docs/reference/fql/user/

Related

How create django model with timestamptz field

I have responce data from API it's look like this
{
"api": {
"results": 1,
"fixtures": {
"65": {
"fixture_id": "65",
"event_timestamp": "1533927600",
"event_date": "2018-08-10T19:00:00+00:00",
"league_id": "2",
"round": "Premier League - 1",
"homeTeam_id": "33",
"awayTeam_id": "46",
"homeTeam": "Manchester United",
"awayTeam": "Leicester",
"status": "Match Finished",
"statusShort": "FT",
"goalsHomeTeam": "2",
"goalsAwayTeam": "1",
"halftime_score": "1 - 0",
"final_score": "2 - 1",
"penalty": null,
"elapsed": "95",
"firstHalfStart": "1533927660",
"secondHalfStart": "1533931380"
}
}
}
}
Now I am trying to build fixture model to store above data in PosgreSql database. I dont understand didnt find any example of builded model with timestamptz field. I need to store event_date key in timestamptz. Can anyone to show me how i should create this field
Django does not have a default timestamp field. However, you can add one by having the following model field:
event_date = models.DateTimeField(auto_now_add=True)
EDIT
Or alternatively, something a little more up to date:
from django.utils import timezone
....
event_date = models.DateTimeField(default=timezone.now)
Make sure its timezone.now and not timzone.now()

How to make query with grouping by foreign keys with ordering by sum of field

For example I have a model:
class Reward(models.Model):
user = models.ForeignKey(UserProfile)
amount = models.IntegerField()
I want to get such statistics:
[{
"user": user1,
"total_amount": 100500, // Sum of amount field of all rewards for this user
"total_rewards": 13 // Count of rewards for this user
},
{
"user": user2,
...
}
]
This list should be ordered by total_amount field. How should I do such query?
You can do that with annotate
from django.db.models import Count, Sum
UserProfile.objects.annotate(
total_amount=Sum('reward__amount'),
total_rewards=Count('reward')
).values('id', 'total_amount', 'total_rewards').order_by('total_amount')

Django REST Framework and related models with one field

I want to create a simple REST API for the following model:
I have a main entity called Product with some fields: name, price...
Also I have a related entity called Keyword with one single field: the keyword.
Each product can have one or more keywords.
I can easily translate this into two django models:
class Product(models.Model):
name = models.CharField("Name of the product", max_length=100)
description = models.TextField("Description of the product")
price = models.IntegerField("Price of the product")
received_at = models.DateField("Received at")
class Keyword(models.Model):
keyword = models.CharField(max_length=50)
product = models.ForeignKey(Product, related_name="keywords")
But I'm lost with serializers.
I want a simple verb like this to create:
POST /products
{
"name": "My product name",
"description": "My product description",
"price": 40,
"received_at": "2015-12-1",
"keywords": ["keyword1", "keyword2"]
}
And the common list all, list one, update and delete:
GET /products
[{"name": "Product 1"...}, {"name": "Product 2"...}]
GET /products/1
{"name": "Product 1", "description": ...}
PUT /products/1
{
"name": "My product name",
"description": "My product description",
"price": 40,
"received_at": "2015-12-1",
"keywords": ["keyword2"]
}
DELETE /products/1
The problem is manage the keywords.
When a new product is created all keywords are created
When a product is updated I have to check which keywords are news and which keywords have been deleted.
Also I want my API can be extensible in the near future with more relations like the "Keyword relation" with a simple string field.
Any ideas?
Try implementing the underlying keywords as members of a set. Then you can simply call the add method with each new keyword and the set will keep it a unique grouping, by the nature of how sets work.
a = set()
a.add('keyword1')
a.add('keyword2')
a.add('keyword1')
print(a)
Output:
set(['keyword1', 'keyword2'])

Django: Tastypie: Updating record after displaying it

I have a very simple model:
class challenge(models.Model):
challenge = models.CharField(max_length=255)
timestamp = models.DateTimeField('date published',null=True,blank=True)
code = models.CharField(max_length=255)
And that model is linked via Tastypie.
By default the timestamp field is empty when you add an entry.
I would like to update the timestamp field with the time that the user first accessed the resource.
For example, if admin added data is:
Challenge: Do something
Timestamp: Null
Code: 123
Then after accessing /api/challenge/1/?format=json to have the output be:
{"challenge": "Do something", "code": "sasdasd", "id": 2, "resource_uri": "/api/challenge/2/", "timestamp": "2015-05-11T12:18:54"}
Is this possible using Tastypie and how?

Message Online Friends

FQL Query
SELECT name, online_presence IN ('active', 'idle') FROM user WHERE online_presence IN ('active', 'idle') AND uid in(SELECT uid2 from friend where uid1= me())
By This Query I am getting friends
Please Anyone Tell Me how can I message these online friends.
Method 1:
Assume:
Your app id is "87741124305"
"4" is one of your friend uid get from FQL query:
SELECT uid, name, online_presence FROM user WHERE online_presence IN
('active', 'idle') AND uid in(SELECT uid2 from friend where uid1=
me())
you want to share a link "https://www.youtube.com/watch?v=VM56POaEEX4"
https://www.youtube.com/facebook_redirect is your app CANVAS_URL
The full link would be:
https://www.facebook.com/dialog/send?app_id=87741124305&to=4&link=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DVM56POaEEX4%26feature%3Dshare&redirect_uri=https%3A%2F%2Fwww.youtube.com%2Ffacebook_redirect
Method 2:
Send email to him.The email address is in the form USERNAME#facebook.com You can get the USERNAME via FQL query:
SELECT uid, username, online_presence FROM user WHERE online_presence
IN ('active', 'idle') AND uid in(SELECT uid2 from friend where uid1=
me())
Please note that, some user doesn't have username. Also, you can't send empty body message.
Method 3:
You may refer to https://developers.facebook.com/docs/chat/ for Jabber/XMPP service.