Django admin customization with non-related models - django

I am a django noob and am trying to figure out how to get the admin module to do something slightly different than the normal operation on a single model. Essentially what I need is to run a query and display the results of the query as a view page and then allow the link to the edit page take the user to an existing model's edit view. 2 of the 3 tables in my query are related, but not all 3.
Example:
select a.foo, a.second_field, b.bar, c.unrelated_field
from a, b, c
where a.primary_key = b.foreign_key
and a.some_value = c.some_value
Note that a and c are not defined as related tables.
I would like to have a view of this query output and have a link to the edit view of the b model as a whole when selected.
I have created a view in the DB for this query and simply created a new model which makes it easy to get the view, but I'm not sure this is even the right approach to start with...but from there I can't seem to figure out how to make this link to the edit page for the B table.
Any pointers or advice on how best to accomplish something like this with django admin would be appreciated!
Using Django 1.3.1 by the way.
Cheers!

You can override change_view in your ModelAdmin so it will construct a list of dicts with all your needed data. Then override change_list.html template to display this data correctly and link it with change_form view for correct model. So it will flawlessly integrate in Django's admin site.
And I don't like DB views as long as it's possible to solve the problem without it. If data can be constructed in Python without massive performance gaps and lots of magic code, it should be processed in Python.

Related

writing a generic listview template to use across different models

I am new to django and am looking for a generic way of displaying all records of a model in a view by writing minimum html.
So ideally what i would like to do is define my model for example customer, add fields like first name, address, credit card no.
Now i would mark which of these fields are to be rendered ( say publicly_visible = false for credit card field). I repeat this for another model like 'products'.
Next i want my view for customer to render a list of all customer records (the credit card column will not be rendered).
I am wondering if there already is a django package which will do this for me?
The other option which i am trying is to try to use a generic listview to do this but not sure how to proceed.
Meet django's "Class Based Views".
You can read the docs here Django Docs on CBV, there are several generic views to accomplish repetitve tasks.
The view you're looking for regarding your question is called ListView.
For others looking for a good solution to generate read-only list views for your models without writing too much code - This did it for me.
https://github.com/miracle2k/django-tables

User-based views in Django 1.6

This is probably a very newbie question however I'm kind of stuck.
I've been looking around for a way to render a user based view for a calendar scheduler however I can't seem to find anything about user-based views in django. It's probably really simple but I'm too blind to spot any documentation about it. Something alongside of a user profile.
I've read through the django book but I cannot recall if there was anything that describes what I'm trying to achieve. Any hints or links would be a great help.
Thank you.
What you are actually are looking for is a DetailView, Django nowdays uses Class Based Views which are kind of self explanatory, there is a ListView which is used to display a list of objects, a DetailView which is used to display details of a specific object and others like FormViews, CreateView, DeleteView etc (you can find more information to this very detailed site apart from Django's own documentation):
http://ccbv.co.uk/
Since you want to display a view for a specified object, this becomes a DetailView, since a User also has a calendar with events, then you also need to fetch the Calendar with the events (but this all breaks down to how your model relations are glued together).
For instance if your user has a Single Calendar (he...should actually) and many events in the calendar, you could declare a Queryset in the DetailView:
http://ccbv.co.uk/projects/Django/1.6/django.views.generic.detail/DetailView/
You can create common template for all users and then populate it with user specific data.
I'm not really sure where you're stuck, or what you're defining as a "user-based view".
The user model is like any other: you can query it, and other models can be related to it. If you just want to show data for the current user, you get that from request.user: otherwise, you can pass the user ID in the URL like any other parameter.

How to add report section to the Django admin?

I want to implement a report section in Django admin. This would mean adding a custom section in the admin homepage where instead of a list of models I would see a list of reports. I want to use Django's admin tables with filters, sorting, everything if possible.
What would be the "best" way of achieving this? I realize this is a "big" question so I'm not asking for code snippets necessarily, a summary of needed actions would be just fine :)
P.S. Be report I mean a "made up" model by custom queries (queryset or how it's called).
P.S.2 Maybe this question should be something like: How to use Django admin tables functionality in own admin view?
P.S.3 Or maybe there is a way of providing to the existing admin interface my own data. This way I don't have to do anything else. I just want to say instead of a model take this data and display it in a nice table which I can sort, filter etc etc.
So you are attempting to add in new pages into the django admin.
This section explains to you exactly how you can do so - https://docs.djangoproject.com/en/dev/ref/contrib/admin/#adding-views-to-admin-sites
The basic idea is to add in new urls that you want in your urls.py as if you are adding urls for your "front end" pages. The key difference is that these new urls you are adding should start with ^admin/ and would look something like ^admin/my_special_link_in_admin and this url will point to your own custom view function at a location you so prefer.
E.g.
(r'^admin/my_special_link_in_admin/$', 'my_custom_admin_app.views.special_admin_page'),
So this is the way for complete customization. There's a very good tutorial which I refer to here - http://brandonkonkle.com/blog/2010/oct/4/django-admin-customization-examples/
In addition, if you don't want to do too much work, consider using Django Admin Plus - https://github.com/jsocol/django-adminplus
Or a django-admin-views - https://github.com/frankwiles/django-admin-views

Django: Best way to handle multiselect US state selection

What is the best way to include in a model to save multi-select US states? I need a user to select several states and save this data in a model (via form OR modelform ).
I tried LocalFlavor USStateField model it doesn't seem to work for me since I can't call it like a regular model.
This django app i've written may help you.

Django Admin Template Overriding: Displaying checkboxselectmultiple widget

Have 2 tables Domain and Group having one to many relationship.
These tables have many to many relationship with User table
On the User admin interface I am rendering the Group and Domain as CheckboxSelectMultiple
widgets.
Is it possible to present this in a table form with 2 columns: Domain in one column and the list of groups belonging to the domain in the other column.
I want to override the fieldset template of the admin. However I am having difficulties knowing which methods/properties I can use with an AdminField.
Thanks
I'm not quite sure I 100% follow what you are trying to display.
AdminField is not documented unfortunately but its a short class, only 18 lines long so you can read it here.
I have a feeling you might be trying to step beyond what the admin allows you to do easily, once you are trying to combine more than two different models on the same page things can get a bit messy and you are soon in the business of customizing the admin by writing custom views and templates.
Am I correct in thinking you want to change the list of the objects? Rather than changing the editing/creating page?
I had similar problem and what I did is that I created new html pages and copied the same code from the Admin Template directory HTML pages to my template directory which will be overridden automatically, and then changed the HTML code to what I wanted to be. hope this is useful.