Reputation: 9269
I have two models one is Employee and other is Asset, with Many to one relation between Asset and Employee. And Asset is added as StackedInline field to Employee Admin interface, Is there anyway I can make Asset as read only field in the Employee Admin.
My intention was to show all the assets the employee is currently holding in the Admin, so that he will not delete it accidentally.
Upvotes: 1
Views: 4924
Reputation: 157
If you only wish to display the assets of an employee, you could change the default django admin change_form.html. (and you may also want to disable the inline first)
To override the default admin templates, copy the admin template folder to your local django template folder(the ${TEMPLATE_DIRS} in your setting.py)
And in change_form.html, there is this block,
{% for inline_admin_formset in inline_admin_formsets %}
{% include inline_admin_formset.opts.template %}
{% endfor %}
Which is used to display the inlines, and what you can do here is, rendering some extra information, such as a list of assets of current employee, to this template, and place them above where the original position of inlines.
And now the question is: how do I render this extra information to this template? This can be done by overriding the change_view() function in your Employee's admin model.
For example, in your admin.py
class EmployeeAdmin(admin.ModelAdmin):
...
def change_view(self, request, object_id, extra_context=None):
assets = Asset.objects.filter(employee=Employee.objects.get(id=object_id))
context_data = {'inlines': assets, }
return super(EmployeeAdmin, self).change_view(request, object_id, extra_context=context_data)
And now back to your admin's change_form.html, user the template tags to display the extra_context from your EmployeeAdmin.
for example,
{% for inline in inlines %}
{{ inline }}
{% endfor %}
{% for inline_admin_formset in inline_admin_formsets %}
{% include inline_admin_formset.opts.template %}
{% endfor %}
Hope this helps, and this is with django 1.2.4
Upvotes: 0
Reputation: 8336
use this code and import it in admin.py as ReadonlyAdmin.This is modified form of readonly admin.
from django import forms
from django.utils.safestring import mark_safe
from datetime import datetime
class ReadOnlyWidget(forms.Widget):
def __init__(self, original_value, display_value):
self.original_value = original_value
self.display_value = display_value
super(ReadOnlyWidget, self).__init__()
def render(self, name, value, attrs=None):
if self.display_value is not None:
return unicode(self.display_value)
return unicode(self.original_value)
def value_from_datadict(self, data, files, name):
return self.original_value
#to make fields foreignkey readonly
class ReadOnlyAdminFields(object):
def get_form(self, request, obj=None):
form = super(ReadOnlyAdminFields, self).get_form(request, obj)
if hasattr(self, 'readonly') and obj is not None:
for field_name in self.readonly:
if field_name in form.base_fields:
if hasattr(obj, 'get_%s_display' % field_name):
display_value = getattr(obj, 'get_%s_display' % field_name)()
else:
display_value = None
if getattr(obj, field_name).__class__ in [unicode , long, int, float, datetime, list]:
form.base_fields[field_name].widget = ReadOnlyWidget(getattr(obj, field_name), display_value)
else:
form.base_fields[field_name].widget = ReadOnlyWidget(getattr(obj, field_name).id, display_value)
form.base_fields[field_name].required = False
return form
Upvotes: 2
Reputation: 2433
Edit: Actually, I don't think this will work for inline models..
Django will be adding native read-only fields in Django 1.1, which should be released around the middle of March.
Read Only Admin Fields (http://www.djangosnippets.org/snippets/937/)
This snippet will allow you to set fields as read-only in the admin.
Upvotes: 3
Reputation: 4884
I think perhaps you can add a custom widget to the fields that's just a basic form except with 'disabled' set on all elements - I cannot tell you how to remove the possibility to add new records though.
Upvotes: 0
Reputation: 1061
I don't think there is a flag in django-admin for that, check out Chapter 18 of the Django book for more details, you'll need to hack the templates by hand
http://www.djangobook.com/en/beta/chapter18/
Upvotes: 0