I'm new to Django and I would like to change the table that is on the admin site.
Now there is an User class and a Group class, I would like to add my own class, 'Games', to the admin site which have the variables 'name' and 'size'.
The class 'Games' is in my models.py.
How do I do this?
Add this to your admin.py file
from django.contrib import admin
from .models import *
class Gamesadmin(admin.ModelAdmin):
class Meta:
model = Games
fields = ('name','size',)
admin.site.register(Games,Gamesadmin)
Do not forget to use your class name correctly . Good Luck.
Related
I have registered some models to display in the admin area, but I would like for some fields to be hidden.
As an example, I have a TeachingClasses model with a BooleanField named 'Status' that is set to True or False depending if the class is open or not. But that is set somewhere else in the app. There is no need to have that field displayed in the admin area when someone wants to create a new class to attend.
As such, is there a way to hide that field in the admin area?
I have tried adding this to the app admin.py file but it did nothing
from django.contrib import admin
class MyModelAdmin(admin.ModelAdmin):
class TeachingClasses:
exclude = ('Status',)
but it's not working?
Any clue if this is the right way?
My model:
class TeachingClasses(models.Model):
name = models.Charfield('Class Name',max_lenght=64)
[...]
status = models.BooleanField('Status',default=True)
What you did is not the correct syntax, you need:
class TeachingClassesAdmin(admin.ModelAdmin):
exclude = ('status',)
admin.site.register(TeachingClasses, TeachingClassesAdmin)
Django doc about how to use exclude.
In the admin.py:
class TeachingClassesAdmin(admin.ModelAdmin):
list_display = ('name',) # plus any other fields you'd like to display
admin.site.register(TeachingClasses, TeachingClassesAdmin)`
I'm working in a form with a m2m field. I want that this field looks like the horizontal interface of the django admin site... ¿how i can do it?
thanks...
You need to use the FilteredSelectMultiple widget
from django.contrib.admin.widgets import FilteredSelectMultiple
from django import forms
from .models import Person
class PersonForm(forms.ModelForm):
some_field = forms.ModelMultipleChoiceField(Person.objects.all(), widget=FilteredSelectMultiple("Person", False, attrs={'rows':'2'}))
class Meta:
model = Person
You will also need to include the Javascript and CSS used in the admin. Here's an example
I'm building a website which contains several languages.
Therefore I need to set verbose names of all attributes in all my model classes (with the internationalizaton-helper). Unfortunately I don't find a way to change the verbose names of the user attributes...
And overwriting every single form isn't really a nice way to do it, right?
Thanks for your help!
Ron
It depends on what you need this for. Generally, verbose_name is only used within the context of the admin. If that's what you need to worry about, then you can create a proxy model and then make your user admin use that:
from django.contrib.auth.models import User
from django.utils.translation import ugettext_lazy as _
class CustomUser(User):
class Meta:
proxy = True
app_label = 'auth'
verbose_name = _('My Custom User')
Then, in admin.py:
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import User
from .models import CustomUser
admin.site.unregister(User)
admin.site.register(CustomUser, UserAdmin)
Since this is the first answer on google when searching for django verbose name I thought I'd correct Chris Pratt's answer from 2012 to be correct for django 2.0
If you only want to update the model name in the Admin panel you can simply add a Meta class to your user model. Note the slight change from Chris' answer.
class Meta:
verbose_name = 'My Custom User'
so i'm using the admin LogEntry object/table to log events in my app. I have a view where i'd like to display each LogEntry.
It would be really great if i could join the LogEntry with the actual objects they represent (so i can display attributes of the object inline with the log entry)
In theory this should be easy as we have the model type and id from the LogEntry but i can't figure out how to join them using a queryset.
i thought i could just grab all the ids of the different objects and make another dictionary for each object type and then join them somehow (maybe zip the lists together?) but that seems dumb and not very djano-ish/pythonic.
does anybody have better suggestions?
** edit **
just want to clarify am not looking to use admin, but roll a custom view and template.
As I know Django uses contenttypes framework to perform logging in admin. So you should create generic relation inside your model and then to show inlines in admin use GenericTabularInline and GenericStackedInline. Please consult with the article.
from django.contrib import admin
from django.contrib.admin.models import LogEntry
from django.contrib.contenttypes.generic import GenericTabularInline
from django import forms
from some_app import models
from some_app.models import Item
class LogForm(forms.ModelForm):
class Meta:
model = LogEntry
class LogInline(GenericTabularInline):
ct_field = 'content_type'
ct_fk_field = 'object_id'
model = LogEntry
extra = 0
class ItemForm(forms.ModelForm):
class Meta:
model = Item
class ItemAdmin(admin.ModelAdmin):
form = ItemForm
inlines = [LogInline,]
admin.site.register(models.Item, ItemAdmin)
and you add to Item:
class Item(models.Model):
name = models.CharField(max_length=100)
logs = generic.GenericRelation(LogEntry)
this change won't create anything in your database, so there is no need to sync
Recent Django versions require to create a proxy for LogEntry:
from django.contrib import admin
from django.contrib.admin.models import LogEntry
from django.contrib.contenttypes.generic import GenericTabularInline
class LogEntryProxy(LogEntry):
content_object = GenericForeignKey('content_type', 'object_id')
class Meta:
proxy = True
class LogInline(GenericTabularInline):
model = LogEntry
extra = 0
class ItemAdmin(admin.ModelAdmin):
inlines = [LogInline,]
admin.site.register(models.Item, ItemAdmin)
In my flatpage admin change list page, mysite.com/admin/flatpages/flatpage/, I can see the fields:
URL
Title
Is there a way to also show the field Site? I associate my flatpages to specific sites. The bad way to do it is by going to the actual Flatpage admin source django/contrib/flatpages/admin.py and create a method which will display sites for a Flatpage on the change list page.
I am basically looking for a way to overwrite a django.contrib application on the admin side.
You don't need to edit flatpages/admin.py. Instead, create a CustomFlatPageAdmin that inherits from the default FlatPageAdmin.
You might want to create a customflatpage app for the following admin.py file, or perhaps you already have a utilities app that you can add it to.
#admin.py
from django.contrib import admin
from django.contrib.flatpages.models import FlatPage
from django.contrib.flatpages.admin import FlatPageAdmin
def get_sites(obj):
'returns a list of site names for a FlatPage object'
return ", ".join((site.name for site in obj.sites.all()))
get_sites.short_description = 'Sites'
class CustomFlatPageAdmin(FlatPageAdmin):
list_display = ('title', 'url', get_sites)
#unregister the default FlatPage admin and register CustomFlatPageAdmin.
admin.site.unregister(FlatPage)
admin.site.register(FlatPage, CustomFlatPageAdmin)