1👍
You could probably design your models slightly different. I don’t know what your other needs are but you could probably change the models to something like this:
#The different languages
class Language(models.Model):
abbreviation = models.CharField(max_length=6)
name = models.CharField(max_length=100)
# The different Lines of the chapters (by sections)
class Line(models.Model):
name = models.CharField(max_length=200)
section = models.IntegerField()
line = models.IntegerField()
language = models.ForeignKey(Language)
text = models.TextField()
You would then have to define just one resource for the Line
model and you would query the language/section/chapter/line with a request similar to:
www.myapp.com/api/v1/line/?language__abbreviation=en§ion=1&chapter=1&line=1
If you don’t want to change your models you can achieve what you want by calling the api with something like:
www.myapp.com/api/v1/lineLanguage/?language__abbreviation=en&line__line=1&line__section=1&line__chapter__name=1
Note that for both cases the resources’ foreign key fields need th have the full=True
argument set, as you need to query the foreign model’s fields.
If you want to have the old url pattern used you can achieve that by defining the override_urls
method in your LineLanguageResource that would contain the pattern that would handle the arguments from the url, pass it to some method that would then pass them to the tastypies’ dispatch_detail
method as request.GET elements.