Django Custom Admin Ordering - django

I have a list of images in my admin area. I would like to add a upp/down button to every field. Clicking on the up field will move the image up in the order of images. So lets say its number 3 on the list and i click on it, it should now move to number 2. and also the same for moving down in the order of the list bly clicking on the down field, button or image.
Any ideas? Please not that I am still new to Django

This snippet will add "up" and "down" links next to every object in admin lists for a given model:
http://www.djangosnippets.org/snippets/998/
I use it in most of my project. I made a little reusable app out of it, by putting it in a separate dictionary (don't forget about the __init__.py file) - # -------- the metaclass portion inside models.py file, # -------- the view inside views.py and # -------- the url config inside urls.py.

You would probably have to create a model and associate it with the images in the admin.
The admin is pretty extensible so if you're familiar with object oriented programming you should be able to overload some of the admin classes. With this in mind you should be able to...
Create a model that has the image/image location and a weight value.
create a custom admin view that utilizes this model.
Resources:
http://www.djangobook.com/en/1.0/chapter17
http://www.djangobook.com/en/beta/chapter18

Related

Django - Datetime field shown as in admin panel

I have a model where I can create objects without a problem from the admin panel, I created a form to do the same directly within the website but I cannot manage to create an object directly from the form because of the error shown in the second picture. It is the "DateTimeField". Best case scenario, someone knows which piece of code is required for the form to display a menu ( as shown in picture 1 ) where the user can click which time and date he wants to.
Thank you very much
Second image:
bootstrap-datepicker might be a good bet : https://bootstrap-datepicker.readthedocs.io/en/latest/. But you will need to manually handle its JS behaviour. Not that simple.
Alternative with some guidance here : https://simpleisbetterthancomplex.com/tutorial/2019/01/03/how-to-use-date-picker-with-django.html

Hide model from main admin list, but allow creation in inline editor

In my Django app, I have an Attribute model which has a many-to-many relationship to a MeasurementMethod model.
I put an inline for MeasurementMethod in the admin interface for Attribute, but I don't think it is useful to have a separate interface for managing MeasurementMethods at all; there's no reason why a user would say, "Gee, I wonder what Attributes can be measured by water displacement."
However, this left no way to create new MeasurementMethods from the inline editor until I found Anton Belonovich's post, which says that I need to admin.site.register(MeasurementMethod) first. I did that, and sure enough the edit and create buttons appeared.
But now on the admin page, where there's a list of apps and the models that can be managed, there's an entry for MeasurementMethod that I don't want.
Is there a way to get rid of it? Or is there a better way to accomplish this?
The solution is to register the MeasurementMethod class with a custom admin class that overrides has_module_permission:
#admin.register(MeasurementMethod)
class MeasurementMethodAdmin(admin.ModelAdmin):
def has_module_permission(self, request):
return False
Then the class can still be edited inline.
ModelAdmin.has_module_permission(request)
Should return True if displaying the module on the admin index page and accessing the module’s index page is permitted, False otherwise. ... Overriding it does not restrict access to the add, change or delete views ...
You could create a custom admin site docs and then override the index method/view. Make sure you register your models with this new admin site and hook it up in the urls.py file.

how to display my own custome message in the top of admin page

want to display my own custom message and link in the top of localhost/admin/module/field before the select fields to change page. I tried with link_display that for displaying each instance obj itself, and i don't want that.
You need to override the change_list.html template for your app, see the documentation.
Money quote for your problem:
For example, if we wanted to add a tool to the change list view for
all the models in an app named my_app, we would copy
contrib/admin/templates/admin/change_list.html to the
templates/admin/my_app/ directory of our project, and make any
necessary changes.

Django admin customization with non-related models

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.

Django-admin with callable functions

Django-admin view allows only for changing the values of model objects. Is it, however, possible to configure or change in an easy way the admin view so that it starts exposing functions on objects? I'm not talking about the functions that can be introduced in the drop-down menu on top of the object list. What I mean is a direct access to functions on model objects?
You can add your own view to the admin site by adding an "^admin/..." url in your url conf. You can use this to extend the admin site relatively easy and expose model methods through your own view. See Creating Custom Admin Views here: http://www.djangobook.com/en/1.0/chapter17/ (and another approach and notes here: https://docs.djangoproject.com/en/dev/ref/contrib/admin/#adding-views-to-admin-sites ).
To add this as a button in the model's "change form" in the admin site, override the change_form.html template for the required models (see https://docs.djangoproject.com/en/dev/ref/contrib/admin/#overriding-admin-templates ).
You will probably want to override the object-tools block, which holds the buttons in the top right side of the page. (In Django 1.3 you can extend the object-tools-items block instead, see: https://code.djangoproject.com/ticket/12694 )
(I am quite sure one can build a nice plugin/app that automatically adds object-tools to a model from a custom "object_tools" property in the ModelAdmin with a list of model methods. Let me know id you find something like this.)