Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed ForeignKey('self') so that extra cruft parameters aren't necess…

…ary. Also refactored the way meta.Admin is handled, so that fields aren't initialized until you manually call meta.Admin.get_field_objs()

git-svn-id: http://code.djangoproject.com/svn/django/trunk@238 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 584cab7bc5125be488c8f755b5c3ce50cf5a5c6f 1 parent 039d121
@adrianholovaty adrianholovaty authored
View
2  django/bin/django-admin.py
@@ -51,7 +51,7 @@ def get_sql_create(mod):
table_output = []
for f in opts.fields:
if isinstance(f, meta.ForeignKey):
- rel_field = f.rel.to.get_field(f.rel.field_name)
+ rel_field = f.rel.get_related_field()
# If the foreign key points to an AutoField, the foreign key
# should be an IntegerField, not an AutoField. Otherwise, the
# foreign key should be the same type of field as the field
View
44 django/core/meta.py
@@ -179,23 +179,7 @@ def __init__(self, module_name='', verbose_name='', verbose_name_plural='', db_t
else:
self.order_with_respect_to = None
self.module_constants = module_constants or {}
- # Alter the admin attribute so that the 'fields' members are lists of
- # field objects -- not lists of field names.
- if admin:
- # Be sure to use admin.copy(), because otherwise we'll be editing a
- # reference of admin, which will in turn affect the copy in
- # self._orig_init_args.
- self.admin = admin.copy()
- for fieldset in self.admin.fields:
- admin_fields = []
- for field_name_or_list in fieldset[1]['fields']:
- if isinstance(field_name_or_list, basestring):
- admin_fields.append([self.get_field(field_name_or_list)])
- else:
- admin_fields.append([self.get_field(field_name) for field_name in field_name_or_list])
- fieldset[1]['fields'] = admin_fields
- else:
- self.admin = None
+ self.admin = admin
# Calculate one_to_one_field.
self.one_to_one_field = None
@@ -508,6 +492,9 @@ def __new__(cls, name, bases, attrs):
# RECURSIVE_RELATIONSHIP_CONSTANT, create that relationship formally.
if f.rel and f.rel.to == RECURSIVE_RELATIONSHIP_CONSTANT:
f.rel.to = opts
+ f.name = (f.rel.name or f.rel.to.object_name.lower()) + '_' + f.rel.to.pk.name
+ f.verbose_name = f.verbose_name or f.rel.to.verbose_name
+ f.rel.field_name = f.rel.field_name or f.rel.to.pk.name
# Add "get_thingie" methods for many-to-one related objects.
# EXAMPLES: Choice.get_poll(), Story.get_dateline()
if isinstance(f.rel, ManyToOne):
@@ -2041,8 +2028,9 @@ def __init__(self, to, to_field=None, rel_name=None, **kwargs):
try:
to_name = to._meta.object_name.lower()
except AttributeError: # to._meta doesn't exist, so it must be RECURSIVE_RELATIONSHIP_CONSTANT
- kwargs['name'] = kwargs['name']
- kwargs['verbose_name'] = kwargs['verbose_name']
+ assert to == 'self', "ForeignKey(%r) is invalid. First parameter to ForeignKey must be either a model or the string %r" % (to, RECURSIVE_RELATIONSHIP_CONSTANT)
+ kwargs['name'] = ''
+ kwargs['verbose_name'] = kwargs.get('verbose_name', '')
else:
to_field = to_field or to._meta.pk.name
kwargs['name'] = kwargs.get('name', to_name + '_id')
@@ -2158,5 +2146,19 @@ def __init__(self, fields, js=None, list_display=None, list_filter=None, date_hi
self.search_fields = search_fields or []
self.save_on_top = save_on_top
- def copy(self):
- return copy.deepcopy(self)
+ def get_field_objs(self, opts):
+ # Returns self.fields, except with fields as Field objects instead of
+ # field names.
+ new_fieldset_list = []
+ for fieldset in self.fields:
+ new_fieldset = [fieldset[0], {}]
+ new_fieldset[1].update(fieldset[1])
+ admin_fields = []
+ for field_name_or_list in fieldset[1]['fields']:
+ if isinstance(field_name_or_list, basestring):
+ admin_fields.append([opts.get_field(field_name_or_list)])
+ else:
+ admin_fields.append([opts.get_field(field_name) for field_name in field_name_or_list])
+ new_fieldset[1]['fields'] = admin_fields
+ new_fieldset_list.append(new_fieldset)
+ return new_fieldset_list
View
6 django/views/admin/main.py
@@ -551,7 +551,7 @@ def _get_template(opts, app_label, add=False, change=False, show_delete=False, f
javascript_imports.extend(['%sjs/getElementsBySelector.js' % ADMIN_MEDIA_PREFIX, '%sjs/dom-drag.js' % ADMIN_MEDIA_PREFIX, '%sjs/admin/ordering.js' % ADMIN_MEDIA_PREFIX])
if opts.admin.js:
javascript_imports.extend(opts.admin.js)
- for _, options in opts.admin.fields:
+ for _, options in opts.admin.get_field_objs(opts):
try:
for field_list in options['fields']:
for f in field_list:
@@ -589,7 +589,7 @@ def _get_template(opts, app_label, add=False, change=False, show_delete=False, f
if opts.admin.save_on_top:
t.extend(_get_submit_row_template(opts, app_label, add, change, show_delete, ordered_objects))
t.append('{% if form.error_dict %}<p class="errornote">Please correct the error{{ form.error_dict.items|pluralize }} below.</p>{% endif %}\n')
- for fieldset_name, options in opts.admin.fields:
+ for fieldset_name, options in opts.admin.get_field_objs(opts):
t.append('<fieldset class="module aligned %s">\n\n' % options.get('classes', ''))
if fieldset_name:
t.append('<h2>%s</h2>\n' % fieldset_name)
@@ -671,7 +671,7 @@ def _get_template(opts, app_label, add=False, change=False, show_delete=False, f
if add:
# Add focus to the first field on the form, if this is an "add" form.
t.append('<script type="text/javascript">document.getElementById("id_%s").focus();</script>' % \
- opts.admin.fields[0][1]['fields'][0][0].get_manipulator_field_names('')[0])
+ opts.admin.get_field_objs(opts)[0][1]['fields'][0][0].get_manipulator_field_names('')[0])
if auto_populated_fields:
t.append('<script type="text/javascript">')
for field in auto_populated_fields:
Please sign in to comment.
Something went wrong with that request. Please try again.