Database table doesnt show up in django admin - python

I'm new to django and I'm wondering why the database table I created with django models won't show up on the admin page.
Here's what I did in a bash window.
And my admin page.
It seems the Cards table has been created but I don't see the table on my admin page and I want to know why. Any advice will be much appreciated.

You have to enter your table name in admin.py file like this
from django.contrib import admin
admin.site.register(ModelName)

Related

How to add actions to Python django admin page

I have a django project in which the the form records the name and the email addresses of the users and I was able to put that data using forms.py and models.py.
What I want to do nextis to create an action through which I can download that in csv file.
My admin page looks like this now and I want to add action right above.
in order to add an action to a model in the admin page you have to create a new class like this and register it with your model:
admin.py
from youSite.views import downloadCSV
from yourSite.models import Info
class infoAdmin(admin.ModelAdmin):
actions =[downloadCSV]
admin.site.register(infoObject, infoAdmin)
You have to create the function in your views and import it into the admin page. It create a new action in that model.
Hope it helps

Django admin panel logout functionality

Is there any way to logout from all devices where I have logged in as admin in Django admin panel?
The site has already been deployed to heroku server
you can manually delete all sessions you have in your database.
from django.contrib.sessions.models import Session
Session.objects.delete()
There is no such option, one idea is to delete that user and recreate super user.
Change superuser password.

How to display Historical table of django-simple-history in Django admin site?

I have implemented Historical tracking of changes to objects in Django using django-simple-history
https://django-simple-history.readthedocs.io/en/2.10.0/index.html
I have mentioned a field history in Model using which I can query an object's history.
history = HistoricalRecords()
There us also a table created in the database with name appname_historicalmodelname.
I want to display this table appname_historicalmodelname in django admin where in we have list of records sorted by history_time.
As I don't have a Model class for that History table, I'm unable to use admin.site.register(HistoricalModelName). How can I display this table in Django admin site?
Django: 1.11
Python: 2.7
django-simple-history comes with SimpleHistoryAdmin, which bolts on a "History" button to your normal admin view that allows you to see the historical records for a particular model instance. However, if you want to be able to create a django admin view for the historical records themselves, you can do the following (assuming base model CustomModel in app my_app):
from django.apps import apps
from django.contrib import admin
HistoricalCustomModel = apps.get_model("my_app", "HistoricalCustomModel")
#admin.register(HistoricalCustomModel)
class HistoricalCustomModelAdmin(admin.ModelAdmin):
...

Django Admin template override

I am following the tutorial on django website to create my first django app.
Now I am stuck trying to override Django Admin template.
My project directory is this:
First I tried creating a new admin template in the surveys app. It works.
Then, I tried with the override function. For this I created at surveys/admin.py the following code:
from django.contrib.admin import AdminSite
from django.utils.translation import ugettext_lazy
class SurveysAdminSite(AdminSite):
site_header = ugettext_lazy('Test administration')
surveys_admin_site = SurveysAdminSite()
And add to computationalMarketing/urls.py the following:
from .admin import surveys_admin_site
urlpatterns = [
path('admin/', surveys_admin_site.urls, name='admin'),
]
It doesn't work, so I search, and tried something different. Add this same previous code to surveys/urls.py. Neither works. Then I rollback the changes to save the code to computationalMarketing/urls.py, but this time I changed the code from surveys/admin.py to computationalMarketing/admin.py (in fact I created the file because it doesn't exists.
It works and now I see the site header that I want, but I get You don't have permission to edit anything. I have seen that is something related with superuser creation, but until now I was able to admin my surveys app without problem, so I believe in some solution related with override properly the admin.py at surveys app
Does anybody knows why this does not works as expected?
You are overriding the complete admin site. You may want to override only specific templates and keep using the default admin site.
A first approach to solve your problem is to add models to your admin site:
surveys/admin.py:
from django.contrib.admin import AdminSite
from django.utils.translation import ugettext_lazy
from surveys.models import OneModel, OtherModel
class SurveysAdminSite(AdminSite):
site_header = ugettext_lazy('Test administration')
surveys_admin_site = SurveysAdminSite()
surveys_admin_site.register(OneModel)
surveys_admin_site.register(OtherModel)
And maybe use ModelAdmin objects to add advanced behavior.
Hope this helps.

Read only usernames in django admin site

I'm using django and its built in admin site for the project I'm working on. (Python 3.4, django 1.7)
For the current project I'm working on I require usernames to be fixed after object creation. I have made a small test project, which literally contains one app, and that app only contains this admin.py:
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import User
class MyAdmin(UserAdmin):
def get_readonly_fields(self,request, obj=None):
ro_fields = list(super().get_readonly_fields(request, obj))
if obj is not None:
ro_fields.append('username')
return ro_fields
admin.site.unregister(User)
admin.site.register(User, MyAdmin)
As expected, the Username can be set in the Add page and is Read Only in the change page. However, I cannot save when on the change page, I receive the message "please correct the errors below" but no field is highlighted. Can anyone suggest a way to work round this problem?
EDIT: as a diferent option I attempted to replace the username with a widget that didn't allow input - no luck there either

Categories