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.
Related
I have a business directory and when i add new company, categories listing mixed style. How can i order categories a to z.
I added my model these meta ordering but didn't worked.
class Category(models.Model):
name = models.CharField(max_length=100)
class Meta:
ordering = ['name']
You can write a class in your admin.py file, like the following:
from django.contrib import admin
from .models import Category
class CategoryAdmin(admin.ModelAdmin):
ordering = ['name']
admin.site.register(Category,CategoryAdmin)
I cant seem to find the correct answer to display 1 foreign key value:
it displays "house label" in the correct "table" it displays the value. but it does not give the perfect "column" name. i would love to display "house_name" in the table.. any idea's?
admin.py
from django.contrib import admin
from .models import UserProfile,House
# Register your models here.
class UserProfileAdmin(admin.ModelAdmin):
def house_label(self, obj):
return obj.house.house_name
list_display = ('user', 'api_key','house_label')
admin.site.register(UserProfile,UserProfileAdmin)
admin.site.register(House)
models.py
from django.db import models
from django.contrib.auth.models import User
# Create your models here.
class House(models.Model):
house_name = models.CharField(max_length=500,blank=False, null = False)
class UserProfile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, related_name ="profile")
api_key = models.CharField(max_length=500, blank=False, null = False)
house = models.ForeignKey(House, on_delete=models.CASCADE, related_name = "house")
If you just want it to say house and display the name, you need a __str__ method on your House model that looks like this:
class House(models.Model):
house_name = models.CharField(max_length=500,blank=False, null = False)
def __str__(self):
return f"{self.house_name}"
and your admin class would be:
class UserProfileAdmin(admin.ModelAdmin):
list_display = ('user', 'api_key', 'house')
If you truly want it to say house_name you just need to rename your admin function and refer to it by that name in your list_display.
class UserProfileAdmin(admin.ModelAdmin):
def house_name(self, obj):
return obj.house.house_name
list_display = ('user', 'api_key','house_name')
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
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)
i have two models
class Reporter(models.Model):
full_name = models.CharField(max_length=70)
# On Python 3: def __str__(self):
def __unicode__(self):
return self.full_name
class Article(models.Model):
pub_date = models.DateField()
headline = models.CharField(max_length=200)
content = models.TextField()
reporter = models.ForeignKey(Reporter)
now in the admin site i want to register the reporter's model and then when it is clicked the Article model objects can be added from within that reporter's model .. is it possible ?? if yes how to accomplish this ??
You need inline model admin. In your admin.py, add the following:
from django.contrib import admin
from .models import Article, Reporter
class ArticleInline(admin.TabularInline):
model = Article
class ReporterAdmin(admin.ModelAdmin):
inlines = [
ArticleInline,
]
admin.site.register(Reporter, ReporterAdmin)