Custom [admin] interfaces with Django? - django

I've been playing around with python/django for the last couple of weeks and whilst the overall structure and makeup of the framework is making sense I'm rather confused on how to create advanced interfaces (in relation to tasks administrators would perform). One trivial example I'm playing around with at the moment is a bulk csv product import for different suppliers which will update various fields of a particular product (keeping track of any changes), creating items where they don't already exist and applying other business logic etc.
With the data successfully in the database and the models reflecting this I envisage a view whereby one could select a supplier from a drop down, which would load all the products silently in the background and display a datagrid on success. The user could then interact with each product individually, for example selecting would display a stacked line chart of pricing history above the datagrid and an optional fly-in panel to the right with options to update prices, add notes etc.
Are there any best practice approaches for achieving something along these lines, does one create custom views/templates or put some heavy lifting into overriding the default Django admin functionality?
Any help is appreciated, thanks in advance.

You can either:
Create a custom django admin action that will appear as an option in the changelist page dropdown menu (of the Supplier model for example.) You can then apply this action to the selected rows. You can also have intermediate pages when using admin actions
You can hook your own views into the django admin for particular models and and then overwrite the appropriate django admin templates to link the two together

Related

Which is most expensive query in django among these?

I have 5 models which needs to be populated using a key from 6th model:
I have two options, please suggest the most effective
Option1: I can fetch all the five one by one and send via context to template and then show the each via html tabs
Option2: Have separate page for each model and fetch when ever user goes to that page, like if user goes to page1 then i fetch data from model1 and so on
Which is good and effective? Like querying only needed or just query all and show up in a html tabs?
You can use django debug toolbar to analyze the cost of your queries.
Also, if the models has a lot of rows and you are rendering it by sending via context in a template then it might be quite slow . Instead you might want to populate asynchronously.

Django Admin LogEntry: how it works in non admin actions?

I am having some struggles how does exactly django.admin.LogEntry objects are created.
Consider the following scenario:
I have a bunch of functions which take a csv file with data that allow me to create multiple objects at one call (just iterate through the file, use the data and if data in given row is correct: create a Model instance). I want to make sure that that each of that creation will be logged.
The question is: django docs are not very descriptive on how does LogEntry works and I am not sure if such actions (not taken in the admin panel itself) will be logged there. Also: will the LogEntries be created for the related objects or I have to trigger them manually?
Does anybody got any experience with such scenarios and can share thoughts about it?
The LogEntry model is in the Admin package and only used by Django admin by default. It is used in the admin layer and not model layer when saving objects. if you want to use it outside the admin, then you will have to manually create the entries yourself. That also means the admin will likely display entries of changes made by normal users so you have to think about how you want the entries displayed

Adding record of page views and details of change to django_admin_log

We are using LogEntry/django_admin_log and recording additions, changes and deletions made from Django Admin. However we have two issues we need to address:
1.) Changes record only the field that changed. Not the old and new values. We would like to add the specific details of all changes.
2.) We would like to record an action in this log every time a page is viewed on the Django Admin panel.
How would it be best to proceed?
We are happy to do some work to extend the existing functionality of this, or we are happy to move to a completely new 3rd part module if necessary, or write our own. But would like some guidance from some experts?
We had similar requirements in terms of keeping history and track of actions done by users with a higher level of detail in terms of values changes. What we ended up doing was creating a new model called "History" which we populated with the user, the name of the model being changed, the instance id and a dictionary called changes showing the name of each field changed and values from - to.
In order to populate the new model, we overrode the save_model function in the admin file for the model we want to track. Regarding the page views, you can overrride the get_fields if "obj" is not None and create an instance of History accordingly.

Django Admin - display 3 different unrelated models on one view using Django admin

So, I am re-making a website for a company and have to use all of their existing databases on my website. I have been using Django and am now to the admin side of things. This is my basic problem:
I have three types of orders (meaning that each type of order is a different type of service so the orders all have different fields from each other), for simplicity I will call them order1, order2 and order3. They are not connected through foreign keys at all. I currently have three different admin classes for each order type and am doing
admin.site.register(Order1, Order1Admin)
admin.site.register(Order2, Order2Admin)
admin.site.register(Order3, Order3Admin)
which is making the correct admin page for each model. However, a requirement given to me is that these three order types must all be listed on the same page. So basically I need to display these three order types side by side on the same page and still maintain the ability to sort by date, I have been looking all over and have found things like inlines, but like I said these have no foreign keys connecting them to the other. How would I do something like
admin.site.register(Order1, Order2, Order3, OrderAdmin)
Are there any options within Django admin for allowing something like this? If not, where is a good starting point to figure out how to make a custom view for these three orders but still have it be within the Django admin framework?

Django admin: two change lists on the same page

I am trying to make an overview page for one of my models I have read through all of http://www.djangobook.com/en/1.0/.chapter17/ and understand how I can add my own custom views for a model to the django admin.
What I am currently trying to do is add multiple filtered change lists (presenting some child models) onto my "overview" page for the model. In these I would like to be able to make use of some of the admin features such as editable fields or actions.
Does anyone have some pointers on how I can best get started with this.
Check this out
He created a proxy model for the original model to avoid the conflict. You can create a new model admin or inherit the old ModelAdmin(as shown in the link)