From 71f55b68b1947e28b1f233625c83ca8b6d11318d Mon Sep 17 00:00:00 2001 From: Robert Wittams Date: Fri, 16 Dec 2005 12:07:27 +0000 Subject: [PATCH] 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 --- .../admin/templates/widget/foreign.html | 4 ++-- .../admin/templatetags/admin_modify.py | 4 ++-- django/contrib/admin/views/main.py | 21 +++++++++++++++++++ django/db/models/__init__.py | 7 ++++--- django/db/models/fields.py | 3 ++- 5 files changed, 31 insertions(+), 8 deletions(-) diff --git a/django/contrib/admin/templates/widget/foreign.html b/django/contrib/admin/templates/widget/foreign.html index d305066066..bdc164e654 100644 --- a/django/contrib/admin/templates/widget/foreign.html +++ b/django/contrib/admin/templates/widget/foreign.html @@ -1,8 +1,8 @@ {% load admin_modify adminmedia %} {% output_all bound_field.form_fields %} {% if bound_field.raw_id_admin %} - FIXURL Lookup + Lookup {% else %} {% if bound_field.needs_add_label %} - FIXURL Add Another + Add Another {% endif %}{% endif %} diff --git a/django/contrib/admin/templatetags/admin_modify.py b/django/contrib/admin/templatetags/admin_modify.py index 48d016164a..136a9c8dcf 100644 --- a/django/contrib/admin/templatetags/admin_modify.py +++ b/django/contrib/admin/templatetags/admin_modify.py @@ -101,13 +101,13 @@ class FieldWrapper(object): self.field = field def needs_header(self): - return not isinstance(self.field, meta.AutoField) + return not isinstance(self.field, models.AutoField) def header_class_attribute(self): return self.field.blank and ' class="optional"' or '' 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 class FormFieldCollectionWrapper(object): diff --git a/django/contrib/admin/views/main.py b/django/contrib/admin/views/main.py index d0b0be3e73..aa069018aa 100644 --- a/django/contrib/admin/views/main.py +++ b/django/contrib/admin/views/main.py @@ -40,6 +40,8 @@ IS_POPUP_VAR = 'pop' # Text to display within changelist table cells if the value is blank. EMPTY_CHANGELIST_VALUE = '(None)' +ADMIN_PREFIX = "/admin/" + def _get_mod_opts(app_label, module_name): "Helper function that returns a tuple of (module, opts), raising Http404 if necessary." try: @@ -96,6 +98,22 @@ def get_model_and_app(path): 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): return render_to_response('admin/index', {'title': _('Site administration')}, context_instance=Context(request)) index = staff_member_required(index) @@ -351,6 +369,9 @@ class AdminBoundField(BoundField): self.cell_class_attribute = ' class="%s" ' % ' '.join(classes) self._repr_filled = False + if field.rel: + self.related_url = url_for_model(field.rel.to) + def _fetch_existing_display(self, func_name): class_dict = self.original.__class__.__dict__ func = class_dict.get(func_name) diff --git a/django/db/models/__init__.py b/django/db/models/__init__.py index 88a3a5ce23..4711e69d4e 100644 --- a/django/db/models/__init__.py +++ b/django/db/models/__init__.py @@ -813,6 +813,7 @@ class AutomaticManipulator(formfields.Manipulator): def save(self, new_data): 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. from django.utils.datastructures import DotExpandedDict params = {} @@ -918,7 +919,7 @@ class AutomaticManipulator(formfields.Manipulator): params[f.attname] = param # 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_cores_given: @@ -976,8 +977,8 @@ class ModelAddManipulator(AutomaticManipulator): super(ModelAddManipulator, self).__init__(follow=follow) class ModelChangeManipulator(AutomaticManipulator): - change = False - add = True + change = True + add = False def __init__(self, obj_key=None, follow=None): assert obj_key is not None, "ChangeManipulator.__init__() must be passed obj_key parameter." diff --git a/django/db/models/fields.py b/django/db/models/fields.py index 75707390eb..cbcb385764 100644 --- a/django/db/models/fields.py +++ b/django/db/models/fields.py @@ -758,7 +758,8 @@ class ForeignKey(Field): # but only for related objects that are in the same app. # EXAMPLE: Poll.add_choice() 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):