Reputation: 45
i've been searching for days without finding any info on this (or a lot of tastypie in general, aside from the docs)...
I'm having a problem with the results that are returned by a very simple tastypie api. The db has a table without a primary key (this is a legacy db and i cannot modify or manage it, so i'm stuck with it). So, in order to work with django, i've set a multitude of:
primary_key=True
in the model fields which combined, make up a unique identifier. So, here's what the model looks like:
class ClientAccount(models.Model):
dt = models.DateField()
date_int = models.IntegerField(primary_key=True)
account = models.CharField(max_length=9, primary_key=True)
product = models.CharField(max_length=5,primary_key=True)
amount = models.IntegerField()
class Meta:
db_table = 'client_account'
managed = False
ordering = ['-date_int']
Here's some data (formatted as pseudo-json for simplicity):
{
dt=2014-08-29
date_int=20140829
account='hello'
product='rice'
amount=10
}
{
dt=2014-08-29
date_int=20140829
account='world'
product='rice'
amount=20
}
{
dt=2014-08-29
date_int=20140829
account='spam'
product='rice'
amount=10
}
{
dt=2014-08-29
date_int=20140829
account='eggs'
product='rice'
amount=20
}
{
dt=2014-08-29
date_int=20140829
account='foo'
product='beans'
amount=5
}
And this is the API:
class ClientResource(ModelResource):
class Meta:
queryset = ClientAccount.objects.all()
resource_name = 'client/account'
filtering = {
'date_int': ALL,
'product': ALL,
}
Now, when i call the api url as follows:
http://127.0.0.1:8000/api/v1/client/account/?product=rice&format=json&offset=0&dt=2014-08-29&limit=1
I get say, the "hello" account. If i refresh (by hitting F5 or clicking on the refresh button on the browser), I may get the "world" account.
Although i get that i may get different results on refreshes (given that the model is only ordered by date_int and there are two records with the same date_int), it seems perhaps unacceptable to get different results after paginating... for example, if i go to the "next" url:
http://127.0.0.1:8000/api/v1/client/account/?product=rice&format=json&offset=1&dt=2014-08-29&limit=1
I may get the "spam" account, or the "eggs" , and if i paginate back, i.e. offset=0, i will get a different account, i.e. not the "hello" account... I may get any of the other accounts, but they will not be in any discernible order. This makes pagination useless as one cannot actually go over the whole set of accounts given that upon each pagination, tastypie delivers different results...
Like i said, i have not been able to find ANY information on this, i would greatly appreciate your help!
Thank you!!
Upvotes: 0
Views: 85
Reputation: 2698
Note the Django docs:
Each model requires exactly one field to have primary_key=True (either explicitly declared or automatically added).
i.e. I expect the problem is with your Django model definition having multiple primary_key=True
fields, and not Tastypie.
The feature request to remedy this is tracked but it is not going to land in time to solve your immediate problem.
Upvotes: 1