Milena Araujo
Milena Araujo

Reputation: 533

Foreign key in tastypie

So, I started using the TastyPie plugin for Django to make a REST api for my project. I was following the getting started guide with my project, but when I got in this point, when I was supposed to put a Foreign Key, it started giving me some errors.

The maior one is this when I do a simple get:

"Reverse for 'api_dispatch_detail' with arguments '()' and keyword arguments '{'pk': 246, 'api_name': 'v1', 'resource_name': 'typep'}' not found."

The code in the resources.py:

class TypeOfPlaceResource(ModelResource):

    class Meta:
        queryset = TypeOfPlace.objects.all()
        resource_name = 'typep'
        allowed_methods = ['get']

class POIResource(ModelResource):

    typep = ForeignKey(TypeOfPlaceResource, 'typep')

    class Meta:
        queryset = PointOfInterest.objects.all()
        resource_name = 'pois'
        filtering = {
            "code1": ALL,
            "code2": ALL,
        }

And the models:

class TypeOfPlace (models.Model):
    name = models.CharField(max_length=100, blank=True)
    code = models.CharField(max_length=20, unique=True)

    def __unicode__(self):
        return self.name

class PointOfInterest(GeoInformation):
    name = models.CharField(max_length=100,blank=True)
    code1 = models.CharField(max_length=4,null=True, unique=True)
    code2 = models.CharField(max_length=4,null=True, unique=True)
    typep = models.ForeignKey(TypeOfPlace)

    def __unicode__(self):
        return self.name

The urls.py

api = Api(api_name='v1')
api.register(TypeOfPlaceResource(), canonical=True)
api.register(POIResource(), canonical=True)

urlpatterns = api.urls

So, am I doing something wrong ? Or missing something ? Any help would be really appreciated ! :D

Upvotes: 4

Views: 4272

Answers (2)

Milena Araujo
Milena Araujo

Reputation: 533

The final answer for my problem is the answer from @manji and @dlrust combined:

"change urlpatterns value to urlpatterns = patterns('', (r'^api/', include(api.urls)),)"

and, after that, "define an authorization in your Meta for the resource".

Hope it's useful for somebody else as it was for me :)

Upvotes: 3

simon
simon

Reputation: 11

It looks like your urlpatterns might be getting overwritten.

urlpatterns += api.urls;

does adding the += like this work? It seems that by assigning directly to urlpatterns you may be unexpectedly clobbering any old assignment you had.

Upvotes: 1

Related Questions