mirror of
https://github.com/django/django.git
synced 2025-10-24 14:16:09 +00:00
Fixed #342 -- added readonly_fields to ModelAdmin. Thanks Alex Gaynor for bootstrapping the patch.
ModelAdmin has been given a readonly_fields that allow field and calculated values to be displayed alongside editable fields. This works on model add/change pages and inlines. git-svn-id: http://code.djangoproject.com/svn/django/trunk@11965 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
@@ -1,11 +1,14 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
import datetime
|
||||
import tempfile
|
||||
import os
|
||||
from django.core.files.storage import FileSystemStorage
|
||||
from django.db import models
|
||||
|
||||
from django.contrib import admin
|
||||
from django.core.files.storage import FileSystemStorage
|
||||
from django.contrib.admin.views.main import ChangeList
|
||||
from django.core.mail import EmailMessage
|
||||
from django.db import models
|
||||
|
||||
|
||||
class Section(models.Model):
|
||||
"""
|
||||
@@ -419,7 +422,47 @@ class CategoryInline(admin.StackedInline):
|
||||
model = Category
|
||||
|
||||
class CollectorAdmin(admin.ModelAdmin):
|
||||
inlines = [WidgetInline, DooHickeyInline, GrommetInline, WhatsitInline, FancyDoodadInline, CategoryInline]
|
||||
inlines = [
|
||||
WidgetInline, DooHickeyInline, GrommetInline, WhatsitInline,
|
||||
FancyDoodadInline, CategoryInline
|
||||
]
|
||||
|
||||
class Link(models.Model):
|
||||
posted = models.DateField(
|
||||
default=lambda: datetime.date.today() - datetime.timedelta(days=7)
|
||||
)
|
||||
url = models.URLField()
|
||||
post = models.ForeignKey("Post")
|
||||
|
||||
|
||||
class LinkInline(admin.TabularInline):
|
||||
model = Link
|
||||
extra = 1
|
||||
|
||||
readonly_fields = ("posted",)
|
||||
|
||||
|
||||
class Post(models.Model):
|
||||
title = models.CharField(max_length=100)
|
||||
content = models.TextField()
|
||||
posted = models.DateField(default=datetime.date.today)
|
||||
|
||||
def awesomeness_level(self):
|
||||
return "Very awesome."
|
||||
|
||||
class PostAdmin(admin.ModelAdmin):
|
||||
readonly_fields = ('posted', 'awesomeness_level', 'coolness', lambda obj: "foo")
|
||||
|
||||
inlines = [
|
||||
LinkInline
|
||||
]
|
||||
|
||||
def coolness(self, instance):
|
||||
if instance.pk:
|
||||
return "%d amount of cool." % instance.pk
|
||||
else:
|
||||
return "Unkown coolness."
|
||||
|
||||
|
||||
class Gadget(models.Model):
|
||||
name = models.CharField(max_length=100)
|
||||
@@ -458,6 +501,7 @@ admin.site.register(Recommendation, RecommendationAdmin)
|
||||
admin.site.register(Recommender)
|
||||
admin.site.register(Collector, CollectorAdmin)
|
||||
admin.site.register(Category, CategoryAdmin)
|
||||
admin.site.register(Post, PostAdmin)
|
||||
admin.site.register(Gadget, GadgetAdmin)
|
||||
|
||||
# We intentionally register Promo and ChapterXtra1 but not Chapter nor ChapterXtra2.
|
||||
|
||||
Reference in New Issue
Block a user