Django Rest Framework: URL with nested resources and two ID's - python

I'm working with DRF and I'm struggling to create a URL like this:
/myApp/languages/<language_id>/countries/<country_id>/
Thanks to a previous question, I know that I can easily use the #action method decorator within DRF router's extra actions, to handle URLs with this format:
/myApp/languages/<language_id>/countries/
However, I haven't found a way to register the new URL that I need, where I include an ID for the second resource:
/myApp/languages/<language_id>/countries/<country_id>/
How can I do it with DRF?

Instead of
/myApp/languages/<language_id>/countries/<country_id>/
Please try below:
/myApp/languages/countries/<language_id>/<country_id>/

Related

Group list items by field value in the Django admin

I'm trying to group the items in the Django admin app by a specific field (e.g. date).
So I've added to the queryset in admin.ModelAdmin.getQueryset() the following:
queryset = queryset.values('date').annotate(Sum('amount'))
But this doesn't work because in this case, a dict is returned instead of a queryset.
I started exploring what's inside the django/contrib/admin folder, and I think something need to be done before sending the object to the template change_list.html.
I'm not sure but I think the class in views/main.py (admin folder) might need some change.
Can anybody confirm that what I'm trying to do is achievable at all?
Please find below a representation of what I'm trying to achieve:
Follow the below example in URL. it's has great way to understand with override django admin with custom queryset and groupby data
https://medium.com/#hakibenita/how-to-turn-django-admin-into-a-lightweight-dashboard-a0e0bbf609ad
I found this useful: https://github.com/xacce/django_admin_grouper
You can simply define the group in ClassAdmin
class RecordAdmin(admin.ModelAdmin):
def regroup_by(self):
return 'category'
The repo overrides Django's change_list_results.html. If your RecordAdmin has method reggroup_by than it inserts a row with the name of the category. If reggroup_by is missing it works as usual.

Add allowed methods to urls for rest_framework_bulk

So this is the first time I use djangorestframework-bulk for implementing batch requests. If I understand correctly, you should be able get this functionality out-of-the-box by just having your *ViewSet classes inherit also from the app's mixins like BulkUpdateModelMixin or viewsets like BulkUpdateAPIView.
As stated on the notes in the repo:
Most API urls have two URL levels for each resource:
url(r'foo/', ...)
url(r'foo/(?P<pk>\d+)/', ...)
The second url however is not applicable for bulk operations because
the url directly maps to a single resource. Therefore all bulk generic
views only apply to the first url.
However, when I try to perform PUT or PATCH bulk requests (haven't tried with DELETE) to an endpoint in my app, like:
http://localhost:8000/api/users/
I get the following error:
PUT http://localhost:8080/api/users/ 405 (METHOD NOT ALLOWED)
Is there any additional configuration I need to do in order for this url to allow PUT and PATCH requests and have my viewset's update() and partial_update() process such requests?
Thanks!!
Your problem is that you've implemented the bulk methods through the BulkUpdateModelMixin, but you don't have a proper router for your ViewSet. You now need, for example, put to go to bulk_update rather than just update.
Check out here for an example of how to set up your own router to allow for bulk update functionality with DRF ViewSets. Basically, map out the verbs to call the proper method.

Best way of integrating url addresses from database to django url patterns

I'm currently playing with django to get acquainted with it. I want to build a litte cms by myself. In an apps model i define a string database-field which represents the url, a field for the bodytext etc. Now i wonder what would be the best way to integrate the url and its corresponding content from the set of data.
Do I have to use views.py for sending database-fields with url data to urls.py?
Is it better to process a database query within urls.py to get the urls?
I hope you can follow me...
Thanks a lot
I've read this question several times and I'm still not sure exactly what you're asking.
The tutorial tells you exactly how to resolve addresses to db entries: capture the components of the URL in your urlconf, pass them to the view, and query your models in the view dependent on the parameters passed from the URL. If this doesn't answer your question, you'll need to explain further what's puzzling you.

Django: Manually getting the view corresponding to a URL

I have a Django project. Given a url, how can I know which view will be dispatched to handle the request?
You want django.core.urlresolvers.resolve, which allows you to map an URL to a view and to keep your URL & view logic separate. This is the opposite of django.core.urlresolvers.reverse which allows you to map a view to a URL.
See the documentation for how to use it!

URLs stored in database for Django site

I've produced a few Django sites but up until now I have been mapping individual views and URLs in urls.py.
Now I've tried to create a small custom CMS but I'm having trouble with the URLs. I have a database table (SQLite3) which contains code for the pages like a column for header, one for right menu, one for content.... so on, so on. I also have a column for the URL. How do I get Django to call the information in the database table from the URL stored in the column rather than having to code a view and the URL for every page (which obviously defeats the purpose of a CMS)?
If someone can just point me at the right part of the docs or a site which explains this it would help a lot.
Thanks all.
You dont have to to it in the flatpage-way
For models, that should be addressable, I do this:
In urls.py I have a url-mapping like
url(r'(?P<slug>[a-z1-3_]{1,})/$','cms.views.category_view', name="category-view")
in this case the regular expression (?P<slug>[a-z1-3_]{1,}) will return a variable called slug and send it to my view cms.views.category_view. In that view I query like this:
#render_to('category.html')
def category_view(request, slug):
return {'cat':Category.objects.get(slug=slug)}
(Note: I am using the annoying-decorator render_to – it is the same as render_to_response, just shorter)
Edit This should be covered by the tutorial. Here you find the url-configuration and dispatching in every detail. The djangobook also covers it. And check pythons regex module.
Of course you can use this code.
Your question is a little bit twisted, but I think what you're asking for is something similar to how django.contrib.flatpages handles this. Basically it uses middleware to catch the 404 error and then looks to see if any of the flatpages have a URL field that matches.
We did this on one site where all of the URLs were made "search engine friendly". We overrode the save() method, munged the title into this_is_the_title.html (or whatever) and then stored that in a separate table that had a URL => object class/id mapping.ng (this means it is listed before flatpages in the middleware list).

Categories

Resources