Foreign key in tastypie

So, I started using the TastyPie plugin for Django to make a REST api for my project. I followed the guide to getting started with my project, but when I received this item , when I had to enter a foreign key, it started giving me some errors.

Major one is this when I make it simple:

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

Code in 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 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 

Urls.py url

 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 is something missing? Any help could be helpful !: D

+4
source share
2 answers

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

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

and then "define authorization in the meta for the resource."

Hope this is helpful to someone else as it was for me :)

+3
source

It looks like your urlpatterns might be overwritten.

 urlpatterns += api.urls; 

adds += how does this work? It seems that by assigning urlpatterns directly, you can unexpectedly knock down any old job you had.

+1
source

All Articles