Now possible to save in a changeform

git-svn-id: http://code.djangoproject.com/svn/django/branches/magic-removal@1691 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Robert Wittams 2005-12-16 12:07:27 +00:00
parent b32aca5d8c
commit 71f55b68b1
5 changed files with 31 additions and 8 deletions

View File

@ -1,8 +1,8 @@
{% load admin_modify adminmedia %} {% load admin_modify adminmedia %}
{% output_all bound_field.form_fields %} {% output_all bound_field.form_fields %}
{% if bound_field.raw_id_admin %} {% if bound_field.raw_id_admin %}
FIXURL<a href="" class="related-lookup" id="lookup_{{ bound_field.element_id }}" onclick="return showRelatedObjectLookupPopup(this);"> <img src="{% admin_media_prefix %}img/admin/selector-search.gif" width="16" height="16" alt="Lookup"></a> <a href="{{bound_field.related_url}}" class="related-lookup" id="lookup_{{ bound_field.element_id }}" onclick="return showRelatedObjectLookupPopup(this);"> <img src="{% admin_media_prefix %}img/admin/selector-search.gif" width="16" height="16" alt="Lookup"></a>
{% else %} {% else %}
{% if bound_field.needs_add_label %} {% if bound_field.needs_add_label %}
FIXURL<a href="/add/" class="add-another" id="add_{{ bound_field.element_id }}" onclick="return showAddAnotherPopup(this);"> <img src="{% admin_media_prefix %}img/admin/icon_addlink.gif" width="10" height="10" alt="Add Another"/></a> <a href="{{bound_field.related_url}}add/" class="add-another" id="add_{{ bound_field.element_id }}" onclick="return showAddAnotherPopup(this);"> <img src="{% admin_media_prefix %}img/admin/icon_addlink.gif" width="10" height="10" alt="Add Another"/></a>
{% endif %}{% endif %} {% endif %}{% endif %}

View File

@ -101,13 +101,13 @@ class FieldWrapper(object):
self.field = field self.field = field
def needs_header(self): def needs_header(self):
return not isinstance(self.field, meta.AutoField) return not isinstance(self.field, models.AutoField)
def header_class_attribute(self): def header_class_attribute(self):
return self.field.blank and ' class="optional"' or '' return self.field.blank and ' class="optional"' or ''
def use_raw_id_admin(self): def use_raw_id_admin(self):
return isinstance(self.field.rel, (meta.ManyToOne, meta.ManyToMany)) \ return isinstance(self.field.rel, (models.ManyToOne, models.ManyToMany)) \
and self.field.rel.raw_id_admin and self.field.rel.raw_id_admin
class FormFieldCollectionWrapper(object): class FormFieldCollectionWrapper(object):

View File

@ -40,6 +40,8 @@ IS_POPUP_VAR = 'pop'
# Text to display within changelist table cells if the value is blank. # Text to display within changelist table cells if the value is blank.
EMPTY_CHANGELIST_VALUE = '(None)' EMPTY_CHANGELIST_VALUE = '(None)'
ADMIN_PREFIX = "/admin/"
def _get_mod_opts(app_label, module_name): def _get_mod_opts(app_label, module_name):
"Helper function that returns a tuple of (module, opts), raising Http404 if necessary." "Helper function that returns a tuple of (module, opts), raising Http404 if necessary."
try: try:
@ -96,6 +98,22 @@ def get_model_and_app(path):
raise Http404 # Couldn't find app raise Http404 # Couldn't find app
_model_urls = {}
def url_for_model(model):
try:
return _model_urls[model]
except KeyError:
comps = model.__module__.split('.')
for mod in models.get_installed_models():
remaining, matched = matches_app(mod, comps)
if matched and len(remaining) > 0:
comps = comps[: - len(remaining)] + remaining[1:]
url = "%s%s/%s/" % (ADMIN_PREFIX, '/'.join(comps) , model.__name__.lower() )
_model_urls[model] = url
return url
raise ImproperlyConfigured('%s is not a model in an installed app' % model.__name__ )
def index(request): def index(request):
return render_to_response('admin/index', {'title': _('Site administration')}, context_instance=Context(request)) return render_to_response('admin/index', {'title': _('Site administration')}, context_instance=Context(request))
index = staff_member_required(index) index = staff_member_required(index)
@ -351,6 +369,9 @@ class AdminBoundField(BoundField):
self.cell_class_attribute = ' class="%s" ' % ' '.join(classes) self.cell_class_attribute = ' class="%s" ' % ' '.join(classes)
self._repr_filled = False self._repr_filled = False
if field.rel:
self.related_url = url_for_model(field.rel.to)
def _fetch_existing_display(self, func_name): def _fetch_existing_display(self, func_name):
class_dict = self.original.__class__.__dict__ class_dict = self.original.__class__.__dict__
func = class_dict.get(func_name) func = class_dict.get(func_name)

View File

@ -813,6 +813,7 @@ class AutomaticManipulator(formfields.Manipulator):
def save(self, new_data): def save(self, new_data):
add, change, opts, klass = self.add, self.change, self.opts, self.model add, change, opts, klass = self.add, self.change, self.opts, self.model
print add, change, opts, klass
# TODO: big cleanup when core fields go -> use recursive manipulators. # TODO: big cleanup when core fields go -> use recursive manipulators.
from django.utils.datastructures import DotExpandedDict from django.utils.datastructures import DotExpandedDict
params = {} params = {}
@ -918,7 +919,7 @@ class AutomaticManipulator(formfields.Manipulator):
params[f.attname] = param params[f.attname] = param
# Create the related item. # Create the related item.
new_rel_obj = related.opts.get_model_module().Klass(**params) new_rel_obj = related.model(**params)
# If all the core fields were provided (non-empty), save the item. # If all the core fields were provided (non-empty), save the item.
if all_cores_given: if all_cores_given:
@ -976,8 +977,8 @@ class ModelAddManipulator(AutomaticManipulator):
super(ModelAddManipulator, self).__init__(follow=follow) super(ModelAddManipulator, self).__init__(follow=follow)
class ModelChangeManipulator(AutomaticManipulator): class ModelChangeManipulator(AutomaticManipulator):
change = False change = True
add = True add = False
def __init__(self, obj_key=None, follow=None): def __init__(self, obj_key=None, follow=None):
assert obj_key is not None, "ChangeManipulator.__init__() must be passed obj_key parameter." assert obj_key is not None, "ChangeManipulator.__init__() must be passed obj_key parameter."

View File

@ -758,7 +758,8 @@ class ForeignKey(Field):
# but only for related objects that are in the same app. # but only for related objects that are in the same app.
# EXAMPLE: Poll.add_choice() # EXAMPLE: Poll.add_choice()
if related.opts.app_label == cls._meta.app_label: if related.opts.app_label == cls._meta.app_label:
setattr(cls, 'add_%s' % rel_obj_name, curry(cls._add_related, rel_class=related.model, rel_field=related.field)) func = lambda self, *args, **kwargs: self._add_related(related.model, related.field, *args, **kwargs)
setattr(cls, 'add_%s' % rel_obj_name, func)
class ManyToManyField(Field): class ManyToManyField(Field):