Django Admin - Custom changelist view - django

I need to add a custom view to the Django Admin. This should be similar to a standard ChangeList view for a certain model, but with a custom result set. (I need to display all models having some date or some other date less than today, but this is not really relevant).
One way I can do this is by using the Admin queryset method, like
class CustomAdmin(admin.ModelAdmin):
...
def queryset(self, request):
qs = super(CustomAdmin, self).queryset(request)
if request.path == 'some-url':
today = date.today()
# Return a custom queryset
else:
return qs
This makes sure that ...
The problem is that I do not know how to tie some-url to a standard ChangeList view.

So you want a second URL that goes to the changelist view so you can check which of the two it was by the requested URL and then change the queryset accordingly?
Just mimick what django.contrib.admin.options does and add another URL to the ModelAdmin.
Should look something like this:
class CustomAdmin(admin.ModelAdmin):
def get_urls(self):
def wrap(view):
def wrapper(*args, **kwargs):
kwargs['admin'] = self # Optional: You may want to do this to make the model admin instance available to the view
return self.admin_site.admin_view(view)(*args, **kwargs)
return update_wrapper(wrapper, view)
# Optional: only used to construct name - see below
info = self.model._meta.app_label, self.model._meta.module_name
urlpatterns = patterns('',
url(r'^my_changelist/$', # to your liking
wrap(self.changelist_view),
name='%s_%s_my_changelist' % info)
)
urlpatterns += super(CustomAdmin, self).get_urls()
return urlpatterns

Related

Is it possible to add 2nd slug to URL path in Django?

I'm using Django version 2.1.
I want to create this type of URL Path in my Project:
www.example.com/bachelor/germany/university-of-frankfurt/corporate-finance
Is it possible to do it in Django?
Yes, say for example that you have a slug for an Author, and one for a Book, you can define it as:
# app/urls.py
from django.urls import path
from app.views import book_details
urlpatterns = [
path('book/<slug:author_slug>/<slug:book_slug>/', book_details),
]
Then the view looks like:
# app/views.py
from django.http import HttpResponse
def book_details(request, author_slug, book_slug):
# ...
return HttpResponse()
The view thus takes two extra parameters author_slug (the slug for the author), and book_slug (the slug for the book).
If you thus query for /book/shakespeare/romeo-and-juliet, then author_slug will contains 'shakespeare', and book_slug will contain 'romeo-and-juliet'.
We can for example look up that specific book with:
def book_details(request, author_slug, book_slug):
my_book = Book.objects.get(author__slug=author_slug, slug=book_slug)
return HttpResponse()
Or in a DetailView, by overriding the get_object(..) method [Django-doc]:
class BookDetailView(DetailView):
model = Book
def get_object(self, queryset=None):
super(BookDetailView, self).get_object(queryset=queryset)
return qs.get(
author__slug=self.kwargs['author_slug'],
slug=self.kwargs['book_slug']
)
or for all views (including the DetailView), by overriding the get_queryset method:
class BookDetailView(DetailView):
model = Book
def get_queryset(self):
qs = super(BookDetailView, self).get_queryset()
return qs.filter(
author__slug=self.kwargs['author_slug'],
slug=self.kwargs['book_slug']
)

Django class based views, is this right approach?

I am using django with django-braces. This is to ask for an opinion if this is the right approach.
For this example I am just trying to return Users and a particular user in json format using CBV.
Views
class ListAllUsers(JSONResponseMixin, DetailView):
model = User
json_dumps_kwargs = {u"indent": 2}
def get(self, request, *args, **kwargs):
object = self.get_object()
context_dict = collections.defaultdict(list)
if not self.kwargs.get("pk"):
for obj in object:
context_dict[obj.id].append ({
u"name": obj.username,
u"email": obj.email
})
else:
context_dict[object.id].append ({
u"name": object.username,
u"email": object.email
})
return self.render_json_response(context_dict)
def get_object(self):
if not self.kwargs.get("pk"):
return User.objects.all()
else:
return get_object_or_404(User, pk=self.kwargs.get("pk"))
urls
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^users/$', ListAllUsers.as_view(), name="users-list"),
url(r'^users/(?P<pk>[0-9]+)/$', ListAllUsers.as_view(), name="users-detail")
]
I know it's a subjective question but I want your opinions please be supportive because I am having a hard time figuring out how to use CBVs optimally.
You're actually using a function-based view within a DetailView class based view (the if statement is the function determining whether to return one object or all the objects). DetailView is a Django class based view for showing detail on one object. You should make this view do that one thing only.
To show a list of objects, you should use ListView.
One solution is to write a second ListView and call it from your urls.py rather than calling your DetailView for both of your url endpoints.

How to use value from URL and request in Django DetailView?

I have a DetailView in django views.py where I want to be able to compare the pk value from the url ex:localhost:8000/myapp/details/3/ and the request.user.id with an if statement.
There is nothing more than the following few lines of code in the view:
class UserDetail(DetailView):
model = Profile
template_name = 'details.html'
Any help would be much appreciated.
Inside a DetailView you have access to self.request, self.args and self.kwargs!
ref.: https://docs.djangoproject.com/en/dev/topics/class-based-views/generic-display/#dynamic-filtering
In your urls.py add something like this:
urlpatterns = [
#...
url(r'^details/(?P<pk>[0-9]+)/$', UserDetail.as_view()),
]
and your UserDetail can now access request.user.id and pk by self.kwargs['pk'] (see reference above: kwargs is name-based, so that you can access it by self.kwargs['name'] and self.args is position-based, so you would access it by self.args[0]).
If I understand your problem correctly, you are trying to manipulate the queryset of the DetailView, to only return the data if the current logged in user is trying to access his page.
If this is true, then you should override get_queryset in your class, like that:
def get_queryset(self):
if self.kwargs['pk'] == self.request.user.id:
return Profile.objects.filter(id=self.request.user.id)
else:
return Profile.objects.none()

Django class-based "method_splitter" - passing 2 slugs as model name and field value, respectively

I want to create a "method_splitter" equivalent using class-based views in order to remove some hard-coding in my URL confs.
I would like to have the following URL's:
ListView: http://mysite.com/<model_name>/
DetailView: http://mysite.com/<model_name>/<field_value>/
where the query for the ListView would be:
<model_name>.objects.all()
and the queryset for the DetailView would be:
<model_name>.objects.get(<field>=<field_Value>)
Currently, my views work as a result of some hardcoding in the url conf, but I would like to find an elegant solution that can scale.
My solution does not give a 404, but displays nothing:
views.py
class ListOrDetailView(View):
def __init__(self, **kwargs):
for key, value in kwargs.iteritems():
setattr(self, key, value)
try: #If/Else instead?
def goToDetailView(self, **kwargs):
m = get_list_or_404(self.kwargs['model']) #Is this even needed?
return DetailView(model=self.kwargs['model'], slug=self.kwargs['slug'], template_name='detail.html', context_object_name='object')
except: #If/Else instead?
def goToListView(self, **kwargs):
q = get_object_or_404(self.kwargs['model'], slug=self.kwargs['slug']) #Is this even needed?
return ListView(model=self.kwargs['model'], template_name='list.html', context_object_name='object_list',)
urls.py of MyApp
url(r'^(?P<model>[\w]+)/?(?P<slug>[-_\w]+)/$', ListOrDetailView.as_view()),
As limelights said, this is a horrible design pattern; the whole point of Django is separation of models and views. If you fear that you might have to write a lot of repetitive code to cover all your different models, trust me that it's not much of an issue with class-based views. Essentially, you need to write this for each of your models you wish to expose like this:
Urls.py:
urlpatterns = patterns('',
url(r'^my_model/$', MyModelListView.as_view(), name="mymodel_list"),
url(r'^my_model/(?P<field_value>\w+)/$', MyModelDetailView.as_view(), name="mymodel_detail"),
)
views.py:
from django.views.generic import ListView, DetailView
class MyModelListView(ListView):
model = MyModel
class MyModelDetailView(DetailView):
model = MyModel
def get_queryset(self):
field_value = self.kwargs.get("field_value")
return self.model.objects.filter(field=field_value)

How Do I Show Django Admin Change List View of foreign key children?

I'm working on an app with a Model hierarchy of Campaign > Category > Account. Ideally, I'd like users to be able to click on a link in the campaign admin list view and go to a URL like "/admin/myapp/campaign/2/accounts/" which will show a Django admin view with all the handy ChangeList amenities but which is filtered to show just the accounts in categories in the specified campaign (ie. Account.object.filter(category__campaign__id = 2)). (Note, categories themselves I'm happy to just be "filters" on this accounts list view).
I can't seem to find any reference to a way to mimic this item-click-goes-to-list-of-foriegn-key-children approach that is common in many other frameworks.
Is it possible? Is there a "better" approach in the django paradigm?
thanks for any help!
This was an interesting question so I whipped up a sample app to figure it out.
# models.py
from django.db import models
class Campaign(models.Model):
name = models.CharField(max_length=20)
def __unicode__(self):
return unicode(self.name)
class Category(models.Model):
campaign = models.ForeignKey(Campaign)
name = models.CharField(max_length=20)
def __unicode__(self):
return unicode(self.name)
class Account(models.Model):
category = models.ForeignKey(Category)
name = models.CharField(max_length=20)
def __unicode__(self):
return unicode(self.name)
# admin.py
from django.contrib import admin
from models import Campaign, Category, Account
class CampaignAdmin(admin.ModelAdmin):
list_display = ('name', 'related_accounts', )
def related_accounts(self, obj):
from django.core import urlresolvers
url = urlresolvers.reverse("admin:<yourapp>_account_changelist")
lookup = u"category__campaign__exact"
text = u"View Accounts"
return u"<a href='%s?%s=%d'>%s</a>" % (url, lookup, obj.pk, text)
related_accounts.allow_tags = True
admin.site.register(Campaign, CampaignAdmin)
admin.site.register(Category)
class AccountAdmin(admin.ModelAdmin):
list_display = ('category', 'name')
list_filter = ('category__campaign',)
admin.site.register(Account, AccountAdmin)
You'll need to replace with the name of your app where the Account ModelAdmin lives.
Note: the list_filter on the AccountAdmin is required since Django 1.2.4, Django 1.1.3 and Django 1.3 beta 1, which introduced protection from arbitrary filtering via URL parameter in the admin.
If i understand you correctly, you want to add a custom field (a callable in your ModelAdmin's list_display) to your CampaignAdmin change_list view.
Your custom field would be a link that takes the category.id of each category in your change_list and generates a link to the desired, filtered admin view, which seems to be the account-change_list in your case:
admin/yourproject/account/?category__id__exact=<category.id>
Assuming category is a field on your Campaign-Model you could add the follwoing method to your CampaignAdmin:
def account_link(self, obj):
return 'Accounts' % (obj.category.id)
account_link.allow_tags = True
And then you add it to the admin's list_display option:
list_display = ('account_link', ...)
It depends a bit on your data model though.
If you want to create a permanent, filtered change_list view that suits your needs, you may take a look at this article: http://lincolnloop.com/blog/2011/jan/11/custom-filters-django-admin/
The other solutions don't pay attention to the filters you already have applied. They are part of the query string and I wanted to retain them as well.
First you need to get a reference to the request, you can do that by wrapping changelist_view or queryset as I did:
class AccountAdmin(ModelAdmin):
model = Account
list_display = ('pk', 'campaign_changelist')
# ...
def queryset(self, request):
self._get_params = request.GET
return super(AccountAdmin, self).queryset(request)
def campaign_changelist(self, obj):
url = reverse('admin:yourapp_account_changelist')
querystring = self._get_params.copy()
querystring['campaign__id__exact'] = obj.campaign.pk
return u'{2}'.format(
url, querystring.urlencode(), obj.campaign)
campaign_changelist.allow_tags = True
And something like that will give you a filter inside the changelist rows. Really helpful. :-)
These are good solutions. I wasn't aware of the auto-filter by url paradigm. Here's another I've discovered which allows you use a custom url scheme:
from consensio.models import Account
from django.contrib import admin
from django.conf.urls.defaults import patterns, include, url
class AccountAdmin(admin.ModelAdmin):
campaign_id = 0;
def campaign_account_list(self, request, campaign_id, extra_context=None):
'''
First create your changelist_view wrapper which grabs the extra
pattern matches
'''
self.campaign_id = int(campaign_id)
return self.changelist_view(request, extra_context)
def get_urls(self):
'''
Add your url patterns to get the foreign key
'''
urls = super(AccountAdmin, self).get_urls()
my_urls = patterns('',
(r'^bycampaign/(?P<campaign_id>\d+)/$', self.admin_site.admin_view(self.campaign_account_list))
)
return my_urls + urls
def queryset(self, request):
'''
Filter the query set based on the additional param if set
'''
qs = super(AccountAdmin, self).queryset(request)
if (self.campaign_id > 0):
qs = qs.filter(category__campaign__id = self.campaign_id)
return qs
And plus you'd need to incorporate the URL link into CampaignAdmin's list view...