Fixed ForeignKey('self') so that extra cruft parameters aren't necessary. 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
This commit is contained in:
parent
039d121227
commit
584cab7bc5
|
@ -51,7 +51,7 @@ def get_sql_create(mod):
|
||||||
table_output = []
|
table_output = []
|
||||||
for f in opts.fields:
|
for f in opts.fields:
|
||||||
if isinstance(f, meta.ForeignKey):
|
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
|
# If the foreign key points to an AutoField, the foreign key
|
||||||
# should be an IntegerField, not an AutoField. Otherwise, the
|
# should be an IntegerField, not an AutoField. Otherwise, the
|
||||||
# foreign key should be the same type of field as the field
|
# foreign key should be the same type of field as the field
|
||||||
|
|
|
@ -179,23 +179,7 @@ class Options:
|
||||||
else:
|
else:
|
||||||
self.order_with_respect_to = None
|
self.order_with_respect_to = None
|
||||||
self.module_constants = module_constants or {}
|
self.module_constants = module_constants or {}
|
||||||
# Alter the admin attribute so that the 'fields' members are lists of
|
self.admin = admin
|
||||||
# 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
|
|
||||||
|
|
||||||
# Calculate one_to_one_field.
|
# Calculate one_to_one_field.
|
||||||
self.one_to_one_field = None
|
self.one_to_one_field = None
|
||||||
|
@ -508,6 +492,9 @@ class ModelBase(type):
|
||||||
# RECURSIVE_RELATIONSHIP_CONSTANT, create that relationship formally.
|
# RECURSIVE_RELATIONSHIP_CONSTANT, create that relationship formally.
|
||||||
if f.rel and f.rel.to == RECURSIVE_RELATIONSHIP_CONSTANT:
|
if f.rel and f.rel.to == RECURSIVE_RELATIONSHIP_CONSTANT:
|
||||||
f.rel.to = opts
|
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.
|
# Add "get_thingie" methods for many-to-one related objects.
|
||||||
# EXAMPLES: Choice.get_poll(), Story.get_dateline()
|
# EXAMPLES: Choice.get_poll(), Story.get_dateline()
|
||||||
if isinstance(f.rel, ManyToOne):
|
if isinstance(f.rel, ManyToOne):
|
||||||
|
@ -2041,8 +2028,9 @@ class ForeignKey(Field):
|
||||||
try:
|
try:
|
||||||
to_name = to._meta.object_name.lower()
|
to_name = to._meta.object_name.lower()
|
||||||
except AttributeError: # to._meta doesn't exist, so it must be RECURSIVE_RELATIONSHIP_CONSTANT
|
except AttributeError: # to._meta doesn't exist, so it must be RECURSIVE_RELATIONSHIP_CONSTANT
|
||||||
kwargs['name'] = kwargs['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['verbose_name'] = kwargs['verbose_name']
|
kwargs['name'] = ''
|
||||||
|
kwargs['verbose_name'] = kwargs.get('verbose_name', '')
|
||||||
else:
|
else:
|
||||||
to_field = to_field or to._meta.pk.name
|
to_field = to_field or to._meta.pk.name
|
||||||
kwargs['name'] = kwargs.get('name', to_name + '_id')
|
kwargs['name'] = kwargs.get('name', to_name + '_id')
|
||||||
|
@ -2158,5 +2146,19 @@ class Admin:
|
||||||
self.search_fields = search_fields or []
|
self.search_fields = search_fields or []
|
||||||
self.save_on_top = save_on_top
|
self.save_on_top = save_on_top
|
||||||
|
|
||||||
def copy(self):
|
def get_field_objs(self, opts):
|
||||||
return copy.deepcopy(self)
|
# 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
|
||||||
|
|
|
@ -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])
|
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:
|
if opts.admin.js:
|
||||||
javascript_imports.extend(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:
|
try:
|
||||||
for field_list in options['fields']:
|
for field_list in options['fields']:
|
||||||
for f in field_list:
|
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:
|
if opts.admin.save_on_top:
|
||||||
t.extend(_get_submit_row_template(opts, app_label, add, change, show_delete, ordered_objects))
|
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')
|
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', ''))
|
t.append('<fieldset class="module aligned %s">\n\n' % options.get('classes', ''))
|
||||||
if fieldset_name:
|
if fieldset_name:
|
||||||
t.append('<h2>%s</h2>\n' % 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:
|
if add:
|
||||||
# Add focus to the first field on the form, if this is an "add" form.
|
# 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>' % \
|
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:
|
if auto_populated_fields:
|
||||||
t.append('<script type="text/javascript">')
|
t.append('<script type="text/javascript">')
|
||||||
for field in auto_populated_fields:
|
for field in auto_populated_fields:
|
||||||
|
|
Loading…
Reference in New Issue