Add view in the bottom of Django modeladmin - python

I have a blog made with Django where I write posts in markdown. I would like to add a view in the bottom of the admin page for each instance of the class Entry (my blog post class) such that I can get a preview of what the markdown looks like, while I'm writing. Just as you get a preview here on Stack Overflow when you create a new post.
I already have an admin class extending ModelAdmin:
class EntryAdmin(admin.ModelAdmin):
list_display = ('title','created')
prepopulated_fields = {'slug': ('title',)}
Is it possible to modify ModelAdmin further, such that it loads a certain html file (blogpost.html) and shows it in the bottom of the admin page?
I made a picture to show exactly what I mean:
NB: I know there are various tools such as Django admin plus, that allows one to add views to the admin interface, but not for each instance of an object.

You can use markdownx for that:
pip install django-markdownx
project settings.py
INSTALLED_APPS =
#. . . .
'markdownx',
]
project urls.py
urlpatterns = [
#[...]
url(r'^markdownx/', include('markdownx.urls')),
]
and then collect static files.
python3 manage.py collectstatic
your models.py
from markdownx.models import MarkdownxField
class MyModel(models.Model):
myfield = MarkdownxField()
your app admin.py
from django.contrib import admin
from markdownx.admin import MarkdownxModelAdmin
from .models import MyModel
admin.site.register(MyModel, MarkdownxModelAdmin)
This should work.

Related

Permission does not show in admin page

I have added an app to my existing django site and to view it, I created an extra permission overview.view. But, I do not see it in my admin page, so I can also not assign this permission to any user. I think I have all files setup correctly, but I guess I am missing something. I have all the same files in the overview folder as I have in other working folders.
I do see the page, but somehow I am not logged in either.
This is my urls.py:
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index')
]
models.py:
from django.db import models
class Overview(models.Model):
class Meta:
permissions = (
('view', "May view the overview"),
)
and (part of) settings.py
INSTALLED_APPS = [
'overview.apps.OverviewConfig'
]
Permissions are stored in the database and so when you add or remove them via the Meta for the model the same is not of course automatically reflected in the database. Permissions are added post the migrations via a post_migrate signal connected from the auth apps appconfig's ready method. See the source code [GitHub]:
post_migrate.connect(
create_permissions,
dispatch_uid="django.contrib.auth.management.create_permissions"
)
Hence when one makes changes to the permissions one needs to run makemigrations and migrate to make sure they are added to the database.

How to set verbose_name for external app model in Django?

I'm trying to set verbose_name for a model SocialAuthUser from django_social.
I've tried to use proxy model, setting its Meta.verbose_name to desired value, but had no success (probably I did it wrong). If it's the way to go, I can provide more details.
It would be great to avoid installing module from pip in editable mode just to replace verbose_name in admin site.
Probably I can replace model name in admin site in some other way?
I thought about adding custom link to admin site, but didn't research this method yet because it feels hacky.
You almost got it right. For your changes on the proxy model to take effect you need to unregister the model from admin site first and then register the proxy model.
The example below is for social_django.Association model.
# admin.py
from django.contrib import admin
from social_django.admin import AssociationOption
from social_django.models import Association
class AssociationProxy(Association):
class Meta:
proxy = True
verbose_name = 'custom model'
app_label = 'social_django'
admin.site.unregister(Association)
admin.site.register(AssociationProxy, AssociationOption)
This assumes you are using the default admin site
# urls.py
from django.contrib import admin
from django.urls import path
urlpatterns = [
path('admin/', admin.site.urls),
]

Showing all models in Django admin panel in development

When I am developing I constantly need to access data from the admin panel but I do not wish to add all models in admin.py since I do not want them to be accessed in production.
Is there a way to show all models in the admin panel in the development environment and hide (part of) them in production automatically?
I think that would be as simple as this:
# my_app/admin.py
from django.contrib import admin
from django.conf import settings
from .models import MyModel, AnotherModel
class MyModelAdmin(admin.ModelAdmin):
pass
class AnotherModelAdmin(admin.ModelAdmin):
pass
# conditional registration of models
if settings.DEBUG:
admin.register(MyModel, MyModelAdmin)
admin.register(AnotherModel, AnotherModelAdmin)

How to change django admin "view site" link to custom absolute url

I have REST API built on Django and JS application. Both are on different domains. How to change django admin "VIEW SITE" link in such way so it will open JS application? I've tried to pass absolute link (https://docs.djangoproject.com/es/1.10/ref/contrib/admin/#django.contrib.admin.AdminSite.site_url), but looks like it does not work - only relative paths allowed
I prefer resetting admin.site.site_url in urls.py, along with other site changes (instead of doing this in an admin.py file, cause a project can have serveral admin.py)
# urls.py
admin.site.site_url = '' # Removes the 'View Site' link
admin.site.site_header = 'My Site'
In Django 1.11.5, it seems that :
from django.contrib import admin and
admin.site.site_url = 'https:....' in the admin.py file is enough
By default , "VIEW SITE" points to '/' i.e localhost:8000 (Default settings assumed).
To change it , use (in admin.py):
admin.site.site_url = "/mySite"
Tested on Django 2.1
There are two solutions I can come up with.
Firstly, you could use custom template admin/base.html. But, reading through the default template, you would have to copy-paste a lot of code just to change a link, which seems like an overkill.
Another solution involves overriding AdminSite. AdminSite has a property called site_url, and it seems like changing it would do the job. So, in essense, you can do something like this:
your_app/admin.py
from django.contrib.admin import AdminSite
from .models import MyModel
class MyAdminSite(AdminSite):
site_url = 'https://yourdomain.com'
admin_site = MyAdminSite(name='myadmin')
your_project/urls.py
from django.conf.urls import url
from myapp.admin import admin_site
urlpatterns = [
url(r'^myadmin/', admin_site.urls),
]
And you should register all your models with your custom admin, not Django's default:
from your_app.admin import admin_site
admin_site.register(MyModel)
Default link to viewsite is http://127.0.0.1:8000/
We can change it to custom url through register it in admin.py
admin.site.site_url = "/mySite"
Example : admin.py
from django.contrib import admin
from auth_app.models import profile
# Register your models here.
admin.site.register(profile)
#register link
admin.site.site_url = "/mySite"
In admin.py, mention your custom path like following
admin.site.site_url = "/<Your Path>"
If your custom url is like "https://example.com/dashboard", You have to put below line in your admin.py
admin.site.site_url = "/dashboard"
You can change "VIEW SITE" link by changing "admin.site.site_url" which is "/" by default:
from django.contrib import admin
print(admin.site.site_url) # /
admin.site.site_url = "/example"

Practical Django Projects - Pages 71 and 80

I am reading the book "Practical Django Projects". It is a nice book. I have a few questions though :
On page 71, there is the following code :
from django.conf.urls.defaults import *
from django.contrib import admin
admin.autodiscover()
from coltrane.models import Entry
entry_info_dict = {
'queryset': Entry.objects.all(),
'date_field': 'pub_date',
}
...
However no variable 'pub_date' has yet been defined in that file !
On page 80, I am being told that I should place two variables DELICIOUS_USER and DELICIOUS_PASSWORD in the Django settings file. I should then call that file with
from django.conf import settings
Where is that Django settings file ? In C:\Python27\Lib\site-packages\django\conf ?
pub_date refers to coltrane.models.Entry attribute pub_date see the source
from django.conf import settings imports your project settings.py so you have to define your settings inside your project/settings.py file. Here are some docs on the official docs about using settings in python code
pub_date is referencing a field defined in the Entry model. Django will look up the field by name later, which is why it's in quotes (otherwise it would trigger an NameError).
In models.py, you should have something like:
class Entry(models.Model):
...
pub_date = models.DateField(...)
The settings file is typically called settings.py, and is located in your project's root folder (next to manage.py, etc.).

Categories

Resources