Adding headers to columns of admin app page in Django - django

I'm customizing the admin panel for my django app and saw that someone pre-1.8 was using list_display = ['join', 'email', 'timestamp', 'updated'] to add a header to the columns of his django app page and separated sections based on his model.
I've tried list_display in django 1.8 and I am failing to see any header names being posted. Is there a new command for this?

Fixed:
from django.contrib import admin
from .models import Join
class JoinAdmin(admin.ModelAdmin):
list_display = ('emails', 'timestamp')
class Meta:
model = Join
admin.site.register(Join, JoinAdmin)
Issue needed to throw in second parameter in the admin.site.register of JoinAdmin

Related

Django-Admin and Django-Summernote create a file field in my forms

I am customizing django-admin with summernote and so far so good, but for some reason it creates a file field in my forms which is never allowed to be empty and I can't update records without uploading dummy files. Please see attachment.
My admin.py code is:
rom django.contrib import admin
from django_summernote.admin import SummernoteModelAdmin
from .models import Post, Category, Tag
# Register your models here.
# admin.site.register(Post)
# POST
#admin.register(Post)
class PostAdmin(SummernoteModelAdmin):
""" Registers the model Post in the Admin Site """
list_display = ('title','publish','status') # Columns to display
list_filter = ('status','created','publish','author') # Filter by on right column
search_fields = ('title','body') # Search in these fields
prepopulated_fields = {'slug': ('title',)} # Prepopulate
filter_horizontal = ('tag',)
raw_id_fields = ('author',)
date_hierarchy = 'publish'
ordering = ('status', 'publish')
summernote_fields = ('body',)
How can I remove the "file" field from there?
Please help.
Many thanks.
I had this same issue today after upgrading Django-Summernote from version 0.8.11.4 to 0.8.11.5 (the current version).
Can solve two ways:
Comment out Django-Summernote -- don't use
Downgrade to version 0.8.11.4 with pip
pip uninstall django-summernote
pip install django-summernote==0.8.11.4

Django Admin tables not displaying correctly

Has anyone seen this before? I've tried making new apps, projects, etc.
All thats in my admin.py file is:
from django.contrib import admin
from . models import UserProfile, Tribe, Membership
# Register your models here.
admin.site.register(Tribe)
admin.site.register(Membership)
admin.site.register(UserProfile)
I've not got any static files or css in the app..?
Create a class that inherit admin.ModelAdmin, update the fields to be shown in the list_display tuple, and register TribeAdmin instead of Tribe. Do the same for the rest.
from django.contrib import admin
from . models import UserProfile, Tribe, Membership
# Register your models here.
class TribeAdmin(admin.ModelAdmin):
list_display = ('field_1', 'field_2',)
admin.site.register(Tribe, TribeAdmin)
# admin.site.register(Membership)
# admin.site.register(UserProfile)
For all the available options, have a look at the documentation or an easy to understand beginner tutorial from the DjangoBook (please note its for an outdated Django Version, but fields works with Django 1.8)
With Django 1.8 you can use.
#admin.register(Tribe)
class TribeAdmin(admin.ModelAdmin):
list_display = ('field',)

django-facebook connect users, but doesn't fill admin tables with any data

I'm successfully using django-facebook to login facebook users
( e.g, after login, the template tag {{ user }} shows the facebook username). however, it doesn't make it to admin interface - and under facebook users there is no data at all.
also, the SQL table django_facebook_facebookcustomuser is showing the user data.
my guess was that i needed to register the FacebookCustomUser model in admin.py, so i did this -
class FacebookCustomUserAdmin(admin.ModelAdmin):
pass
admin.site.register(models.FacebookCustomUser, FacebookCustomUserAdmin)
# and commented out the user admin inteface -
#admin.site.register(models.FacebookUser, FacebookUserAdmin)
it solved the problem, but in a very hackish way...
I now have a 'users' table, showing the different users, but the 'open graph share' and 'like' sections are still not populated with any data from the db.
when i actually try to pass something inside the CustomUser class, as in the original code -
class FacebookCustomUserAdmin(admin.ModelAdmin):
list_display = ('user_id', 'name', 'facebook_id',)
search_fields = ('name',)
i get the following http500 error -
FacebookCustomUserAdmin.list_display[0], 'user_id' is not a callable or an attribute of 'FacebookCustomUserAdmin' or found in the model 'FacebookCustomUser'.
does anyone have an idea on how i can correctly register the facebook users ( as well as the other django-facebook models ) to the admin site?
thanks a lot!
Two pointers:
Extend UserAdmin instead of ModelAdmin.
Use id instead of user_id.
Try this:
from django.contrib import admin
from django_facebook import models
from django.contrib.auth.admin import UserAdmin
class AccountAdmin(UserAdmin):
list_display = ('id', 'username', 'email', 'facebook_id')
admin.site.register(models.FacebookCustomUser, AccountAdmin)
In settings.py you will need to add the following options to make sure likes and friends are saved:
FACEBOOK_STORE_LIKES = True
FACEBOOK_STORE_FRIENDS = True

How do you create a class using (admin.ModelAdmin) for a polling app

I am working through a Django tutorial at http://lightbird.net/dbe/todo_list.html . I completed Django's official tutorial. When I attempted to sync
from django.db import models
from django.contrib import admin
class Item(models.Model):
name = models.CharField(max_length=60)
created = models.DateTimeField(auto_now_add=True)
priority = models.IntegerField(default=0)
difficult = models.IntegerField(default=0)
class ItemAdmin(admin.ModelAdmin):
list_display = ["name", "priority", "difficult", "created", "done"]
search_fields = ["name"]
admin.site.register(Item, ItemAdmin)
The terminal came back with an error that said admin was not defined. What am I doing wrong? How do I define admin?
Update: I added the whole models file as it looks now
The Django documentation lists multiple steps that need to be done to activate the admin site:
Add 'django.contrib.admin' to your INSTALLED_APPS setting.
The admin has four dependencies - django.contrib.auth, django.contrib.contenttypes, django.contrib.messages and
django.contrib.sessions. If these applications are not in your
INSTALLED_APPS list, add them.
Add django.contrib.messages.context_processors.messages to TEMPLATE_CONTEXT_PROCESSORS and MessageMiddleware to
MIDDLEWARE_CLASSES. (These are both active by default, so you only
need to do this if you’ve manually tweaked the settings.)
Determine which of your application’s models should be editable in the admin interface.
For each of those models, optionally create a ModelAdmin class that encapsulates the customized admin functionality and options for
that particular model.
Instantiate an AdminSite and tell it about each of your models and ModelAdmin classes.
Hook the AdminSite instance into your URLconf. lists a couple of things that you must do the enable Django's admin site.
If you haven't done the first two items, syncdb might complain because it can't find the admin app itself.

Left Join in Django Model Multi-table Inheritance in Admin app

Scenario:
Let's say I want to extend Django's User model using multi-table inheritance. Let's say the model I created for that is called CustomUser.
Now let's assume that there are already existing records in the database corresponding to the User model and the table corresponding to the CustomUser model is still empty.
Now I want CustomUser model to be accessible from the Django's Admin app. What I noticed is only User model records which has a corresponding record in the CustomUser table is included in the change list of CustomUser, as if an INNER JOIN is being done behind the scene in the query... (I checked using connection.queries and it was indeed an INNER JOIN).
Now I want to change this behaviour so that a LEFT JOIN is done to retrieve records pertaining to CustomerUser.
How can I achieve this in Django?
Thank you very much!
Put below in any working admin.py.
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
#### below imports your custom user model
from accounts.models import CustomUser
admin.site.unregister(User)
class UserProfileInline(admin.StackedInline):
model = CustomUser
class UserProfileAdmin(UserAdmin):
inlines = [ UserProfileInline, ]
admin.site.register(User, UserProfileAdmin)
Above will show your CustomUser model in User in admin. now you can access User fields from UserProfileAdmin by User__first_name etc. pardon for bad english.