django admin site models not showing - django

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)

Related

How to pass value as string in ForeignKey and not integer?

IMPORT PHOTO
I tried to import my model in admin to csv file and the output of the foreignkeys are integers(see Department and Status fields in photo above). How can i convert that into string?
You can check how to add relationship fields the documentation. It suggests adding two classes: a resource class to define which fields are exported and an admin class that connects the resource class to the admin interface. In your case admin.py may look like
from django.contrib import admin
from models import *
from import_export.admin import ImportExportModelAdmin
from import_export import resources
class HrmResource(resources.ModelResource):
class Meta:
model = hrm
fields = ('name', 'place', 'department__nm_dept')
class HrmAdmin(ImportExportModelAdmin):
resource_class = HrmResource
admin.site.register(r_dept)
admin.site.register(hrm, HrmAdmin)
and models.py like
from django.db import models
class r_dept(models.Model):
nm_dept = models.CharField(max_length=200)
def __str__(self):
return self.nm_dept
class hrm(models.Model):
name = models.CharField(max_length=200)
place = models.CharField(max_length=200)
department = models.ForeignKey('r_dept')
def __str__(self):
return self.name

InlineModelAdmin not showing up on admin page

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)

Django MPTT Giving DatabaseError When Saving

I have installed MPTT for Django, put it in "installed apps," set up my files, and synced my database. My model shows in admin, but when I click save after trying to add a category I get the following error:
DatabaseError at /admin/myapp/category/add/
relation "django_admin_log" does not exist
LINE 1: INSERT INTO "django_admin_log" ("action_time", "user_id", "c...
Here are my files:
Models:
from django.db import models
from django.contrib.auth.models import User
from mptt.models import MPTTModel, TreeForeignKey
class Category(MPTTModel):
user = models.ForeignKey(User)
name = models.CharField(max_length=30, unique=True)
parent = TreeForeignKey('self', null=True, blank=True, related_name='children')
class MPTTMeta:
order_insertion_by = ['name']
def __unicode__(self):
return self.name
Admin:
from django.contrib import admin
from myapp.models import Category
from mptt.admin import MPTTModelAdmin
admin.site.register(Category, MPTTModelAdmin)
Is it possible, that you've enabled the admin logs feature without running syncdb afterwards? Here you'll find a very similar question.

django foreign key

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.

How to implement admin meta fields in Django 1.3 User profile?

from django.db import models
from django.contrib.auth.models import User
class MySiteProfile(models.Model):
# This is the only required field
user = models.ForeignKey(User, unique=True)
# The rest is completely up to you...
favorite_band = models.CharField(max_length=100, blank=True)
favorite_cheese = models.CharField(max_length=100, blank=True)
lucky_number = models.IntegerField()
The problem is that
User._meta.admin and MySiteProfile._meta.admin both return NoneType. I've dropped and recreated whole database, but no new fields appeared in admin panel; AUTH_PROFILE_MODULE is set.
There are a few ways you can make your MySiteProfile show up in the admin. One is to simply register your model with the admin, and it will show up under the app name it resides in.
Another is to unregister the contrib user from admin and instead load yours:
#admin.py
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import User
from my_app.models import MySiteProfile
class MySiteProfileAdmin(UserAdmin):
def __init__(self,*args,**kwargs):
super(MySiteProfileAdmin).__init__(*args,**kwargs)
fields = list(UserAdmin.fieldsets[0][1]['fields'])
fields.append('favorite_band')
fields.append('favorite_cheese')
fields.append('lucky_number')
UserAdmin.fieldsets[0][1]['fields']=fields
admin.site.unregister(User)
admin.site.register(MySiteProfile, MySiteProfileAdmin)
There are quite a few articles around on this topic, but hope that helps you out.