I have
from django.db import models
from django.contrib import admin
# Create your models here.
class Author(models.Model):
name = models.CharField(max_length=10)
class Book(models.Model):
author = models.ForeignKey(Author, null=True, blank=True)
title = models.CharField(max_length=10)
class BookInline(admin.TabularInline):
model = Book
class AuthorAdmin(admin.ModelAdmin):
inlines = [
BookInline,
]
registered on my admin page, however books are not showing up in the author's admin page. Am I misunderstanding how this will work? I want to be able to add and remove books from the author on the admin page.
This is what you should have in admins.py:
from django.contrib import admin
from models import Author, Book
class BookInline(admin.StackedInline):
model = Book
class AuthorAdmin(admin.ModelAdmin):
inlines = [ BookInline ]
admin.site.register(Author, AuthorAdmin)
admin.site.register(Book)
You probably forgot to include 'AuthorAdmin' in this line:
admin.site.register(Author, AuthorAdmin)
Related
How to have multiple tables(queryset) on one page in django admin.
For example:
When i go to the company page, i can see the list of departments in the company, i can also see the list of employees in the company.
You can use InlineModelAdmin objects to implement this, although if you want to do nested inlines you should check out this post. Although as that post says:
...it would be a kind of convoluted design to implement.
You didn't provide any code here so the best I can do is guess your model relationships.
models.py
from django.db import models
class Department(models.Model):
name = models.CharField(max_length=250)
...
class Employee(models.Model):
name = models.CharField(max_length=250)
...
class Company(models.Model):
name = models.CharField(max_length=250)
departments = models.ForeignKey(Department)
employees = models.ForeignKey(Employee)
...
admin.py
from django.contrib import admin
class EmployeeInline(admin.StackedInline):
model = Employee
class DepartmentInline(admin.StackedInline):
model = Department
class CompanyAdmin(admin.ModelAdmin):
list_display = ('name')
inlines = [DepartmentInline, EmployeeInline]
admin.site.register(CompanyAdmin)
I have the following in my models.py:
from django.db import models
class LabName(models.Model):
labsname=models.CharField(max_length=30)
def __unicode__(self):
return self.labsname
class ComponentDescription(models.Model):
lab_Title=models.ForeignKey('Labname')
component_Name = models.CharField(max_length=30)
description = models.CharField(max_length=20)
purchased_Date = models.DateField()
status = models.CharField(max_length=30)
to_Do = models.CharField(max_length=30,blank=True)
remarks = models.CharField(max_length=30)
def __unicode__(self):
return self.component
I have the following in my admin.py:
from django.contrib import admin
from Lab_inventory.models import ComponentDescription,LabName
class ComponentDescriptionAdmin(admin.ModelAdmin):
list_display= ('lab_Title','component_Name','description','purchased_Date','status','to_Do','remarks')
list_filter=('lab_Title','status','purchased_Date')
admin.site.register(LabName)
admin.site.register(ComponentDescription,ComponentDescriptionAdmin)
What I want is to display the fields under the component description to be displayed under the lab title(the fields related to each lab title by should be displayed under that lab name)
What you are doing with list_display and list_filter pertain to the list that is shown in the admin screen where the list of LabName objects are listed.
Assuming one LabName has one-to-many ComponentDescription entities, you need Django's InlineModelAdmin to display the list of ComponentDescription objects belonging to LabName within the admin page for a specific LabName entity. The code would be of the following structure:
from django.contrib import admin
from Lab_inventory.models import ComponentDescription,LabName
class ComponentDescriptionInline(admin.TabularInline):
model = ComponentDescription
class LabNameAdmin(admin.ModelAdmin):
inlines = [
ComponentDescriptionInline,
]
admin.site.register(LabName, LabNameAdmin)
where TabularInline is a subclass of the generic InlineModelAdmin.
i have a site with django, is showing a basic view, and the admin site,
but when i log into the admin site i cannot see the models:
this are my models.py
from django.db import models
# Create your models here.
class Poll(models.Model):
question = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
class Admin:
pass
class Choice(models.Model):
poll = models.ForeignKey(Poll)
choice = models.CharField(max_length=200)
votes = models.IntegerField()
class Admin:
pass
so im referencing the admin from my db scheme,
but cannot see this tables in my admin,
what is missing?
thanks!
You need to create a file called admin.py and register any models you want to be accessible from the Django admin:
from django.contrib import admin
from myproject.myapp.models import MyModel
class MyModelAdmin(admin.ModelAdmin):
pass
admin.site.register(MyModel, MyModelAdmin)
Having a such model below, how can I require atleast one book to be added while creating/editting Author instance at admin panel?
#models.py
class Author(models.Model):
name = models.CharField(max_length=100)
class Book(models.Model):
author = models.ForeignKey(Author)
title = models.CharField(max_length=100)
#admin.py
class BookInline(admin.TabularInline):
model = Book
class AuthorAdmin(admin.ModelAdmin):
inlines = [
BookInline,
]
Matthew Flanagan has a formset class that will do just that: http://wadofstuff.blogspot.com/2009/08/requiring-at-least-one-inline-formset.html
Hope that helps you out.
My purpose is to see at the admin site only user name, email and phone number.
I've create UserProfile by extending User model:
model.py
from django.db import models
from django.contrib.auth.models import User
class UserProfile(models.Model):
user = models.ForeignKey(User, unique=True)
name = models.CharField(max_length=50, null=True,blank=True)
address = models.CharField(max_length=50, null=True,blank=True)
phone = models.CharField(max_length=20, null=True,blank=True)
country = models.CharField(max_length=20, null=True,blank=True)
state = models.CharField(max_length=20, null=True,blank=True)
zip = models.CharField(max_length=10, null=True,blank=True)
code = models.CharField(max_length=40, null=True)
def user_email(self):
return self.user.email
admin.py
from myApp.models import UserProfile
from django.contrib import admin
class UserProfileAdmin(admin.ModelAdmin):
fields = ('name','phone',)
list_display = ('name','user_email',)
admin.site.register(UserProfile, UserProfileAdmin)
so on the list_display it works, I can see only the columns I've chosen, but when I add 'user_email' ( fields = ('name','user_email', 'phone',) )to fields I get when I try to go to admin site:
'UserProfileAdmin.fields' refers to field 'user_email' that is missing from the form.
Fields on a related model use two underscores. Dunno if it'll work in the admin though.
list_display = ('name','user__email',)
Just because I recently used it and you maybe want this, too: If you wan't to add an inline admin to the "User" admin page in Django you can do this (at least in Django 1.3) by doing:
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import User
from models import UserProfile
class UserProfileInlineAdmin(admin.StackedInline):
model = UserProfile
class MyUserAdmin(UserAdmin):
inlines = [ UserProfileInlineAdmin ]
admin.site.unregister(User)
admin.site.register(User, MyUserAdmin)
You can't put editable fields from a related model into an admin form, without using inlines. You can show the field as a readonly value: just add it to readonly_fields.