Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

newforms-admin: Backwards-incompatible change: Refactored prepopulate…

…_from. There's no longer a 'prepopulate_from' option on database Field classes. Instead, there's a new 'prepopulated_fields' option on the 'class Admin'. This should be a dictionary mapping field names (as strings) to lists/tuples of field names that prepopulate them

git-svn-id: http://code.djangoproject.com/svn/django/branches/newforms-admin@4446 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 126c6cef5227ae91c2c3ea92eba220735e1bc411 1 parent 54d2c54
Adrian Holovaty adrianholovaty authored
13 django/contrib/admin/options.py
View
@@ -33,8 +33,9 @@ def unquote(s):
return "".join(res)
class AdminForm(object):
- def __init__(self, form, fieldsets):
+ def __init__(self, form, fieldsets, prepopulated_fields):
self.form, self.fieldsets = form, fieldsets
+ self.prepopulated_fields = [{'field': form[field_name], 'dependencies': [form[f] for f in dependencies]} for field_name, dependencies in prepopulated_fields.items()]
def __iter__(self):
for fieldset in self.fieldsets:
@@ -109,6 +110,7 @@ class ModelAdmin(object):
js = None
fields = None
raw_id_fields = ()
+ prepopulated_fields = {}
def __init__(self, model):
self.model = model
@@ -145,9 +147,8 @@ def javascript(self, request, fieldsets):
"""
from django.conf import settings
js = ['js/core.js', 'js/admin/RelatedObjectLookups.js']
- # TODO: This.
- #if auto_populated_fields:
- #js.append('js/urlify.js')
+ if self.prepopulated_fields:
+ js.append('js/urlify.js')
if self.opts.has_field_type(models.DateTimeField) or self.opts.has_field_type(models.TimeField) or self.opts.has_field_type(models.DateField):
js.extend(['js/calendar.js', 'js/admin/DateTimeShortcuts.js'])
if self.opts.get_ordered_objects():
@@ -319,7 +320,7 @@ def add_view(self, request, form_url='', post_url_continue='../%s/'):
c = template.RequestContext(request, {
'title': _('Add %s') % opts.verbose_name,
- 'adminform': AdminForm(form, self.fieldsets_add(request)),
+ 'adminform': AdminForm(form, self.fieldsets_add(request), self.prepopulated_fields),
'oldform': oldforms.FormWrapper(model.AddManipulator(), {}, {}),
'is_popup': request.REQUEST.has_key('_popup'),
'show_delete': False,
@@ -410,7 +411,7 @@ def change_view(self, request, object_id):
c = template.RequestContext(request, {
'title': _('Change %s') % opts.verbose_name,
- 'adminform': AdminForm(form, self.fieldsets_change(request, object_id)),
+ 'adminform': AdminForm(form, self.fieldsets_change(request, object_id), self.prepopulated_fields),
'oldform': oldforms.FormWrapper(model.ChangeManipulator(object_id), {}, {}),
'object_id': object_id,
'original': obj,
18 django/contrib/admin/templates/admin/change_form.html
View
@@ -83,10 +83,20 @@
<script type="text/javascript">document.getElementById("{{ adminform.first_field.auto_id }}").focus();</script>
{% endif %}
-{% if auto_populated_fields %}
- <script type="text/javascript">
- {% auto_populated_field_script auto_populated_fields change %}
- </script>
+{# JavaScript for prepopulated fields #}
+
+{% if add %}
+<script type="text/javascript">
+{% for field in adminform.prepopulated_fields %}
+ document.getElementById("{{ field.field.auto_id }}").onchange = function() { this._changed = true; };
+ {% for dependency in field.dependencies %}
+ document.getElementById("{{ dependency.auto_id }}").onkeyup = function() {
+ var e = document.getElementById("{{ field.field.auto_id }}");
+ if (!e._changed) { e.value = URLify({% for innerdep in field.dependencies %}document.getElementById("{{ innerdep.auto_id }}").value{% if not forloop.last %} + ' ' + {% endif %}{% endfor %}, {{ field.field.field.max_length }}); }
+ }
+ {% endfor %}
+{% endfor %}
+</script>
{% endif %}
</div>
2  django/contrib/admin/views/main.py
View
@@ -109,7 +109,6 @@ def original_url(self):
def render_change_form(model_admin, model, manipulator, context, add=False, change=False, form_url=''):
opts = model._meta
app_label = opts.app_label
- auto_populated_fields = [f for f in opts.fields if f.prepopulate_from]
original = getattr(manipulator, 'original_object', None)
ordered_objects = opts.get_ordered_objects()
inline_related_objects = opts.get_followed_related_objects(manipulator.follow)
@@ -120,7 +119,6 @@ def render_change_form(model_admin, model, manipulator, context, add=False, chan
'has_change_permission': context['perms'][app_label][opts.get_change_permission()],
'has_file_field': opts.has_field_type(models.FileField),
'has_absolute_url': hasattr(model, 'get_absolute_url'),
- 'auto_populated_fields': auto_populated_fields,
'ordered_objects': ordered_objects,
'inline_related_objects': inline_related_objects,
'form_url': form_url,
10 django/core/management.py
View
@@ -891,8 +891,6 @@ def get_validation_errors(outfile, app=None):
from PIL import Image
except ImportError:
e.add(opts, '"%s": To use ImageFields, you need to install the Python Imaging Library. Get it at http://www.pythonware.com/products/pil/ .' % f.name)
- if f.prepopulate_from is not None and type(f.prepopulate_from) not in (list, tuple):
- e.add(opts, '"%s": prepopulate_from should be a list or tuple.' % f.name)
if f.choices:
if not hasattr(f.choices, '__iter__'):
e.add(opts, '"%s": "choices" should be iterable (e.g., a tuple or list).' % f.name)
@@ -979,6 +977,14 @@ def get_validation_errors(outfile, app=None):
# Check admin attribute.
if opts.admin is not None:
+ # prepopulated_fields
+ if not isinstance(opts.admin.prepopulated_fields, dict):
+ e.add(opts, '"%s": prepopulated_fields should be a dictionary.' % f.name)
+ else:
+ for field_name, field_list in opts.admin.prepopulated_fields.items():
+ if not isinstance(field_list, (list, tuple)):
+ e.add(opts, '"%s": prepopulated_fields "%s" value should be a list or tuple.' % (f.name, field_name))
+
# list_display
if not isinstance(opts.admin.list_display, (list, tuple)):
e.add(opts, '"admin.list_display", if given, must be set to a list or tuple.')
6 django/db/models/fields/__init__.py
View
@@ -68,9 +68,8 @@ class Field(object):
def __init__(self, verbose_name=None, name=None, primary_key=False,
maxlength=None, unique=False, blank=False, null=False, db_index=False,
core=False, rel=None, default=NOT_PROVIDED, editable=True,
- prepopulate_from=None, unique_for_date=None, unique_for_month=None,
- unique_for_year=None, validator_list=None, choices=None, radio_admin=None,
- help_text='', db_column=None):
+ unique_for_date=None, unique_for_month=None, unique_for_year=None,
+ validator_list=None, choices=None, radio_admin=None, help_text='', db_column=None):
self.name = name
self.verbose_name = verbose_name
self.primary_key = primary_key
@@ -79,7 +78,6 @@ def __init__(self, verbose_name=None, name=None, primary_key=False,
self.core, self.rel, self.default = core, rel, default
self.editable = editable
self.validator_list = validator_list or []
- self.prepopulate_from = prepopulate_from
self.unique_for_date, self.unique_for_month = unique_for_date, unique_for_month
self.unique_for_year = unique_for_year
self._choices = choices or []
Please sign in to comment.
Something went wrong with that request. Please try again.