def _form_for_model(model, instance=None, form=forms.BaseForm, fields=None, post=None, formfield_callback=widgets.form_field, lock_fields=[], read_only=False): if hasattr(model.Admin, "form_class"): if model.Admin.form_class: return model.Admin.form_class(post, instance=instance) opts = model._meta field_list = [] default_widgets={} if hasattr(model.Admin, "default_widgets"): default_widgets=model.Admin.default_widgets if callable(default_widgets): default_widgets(instance) for f in opts.fields + opts.many_to_many: if not f.editable: continue if fields and not f.name in fields: continue # if post and not f.name in post.keys(): # continue current_value = None if instance: try: current_value=f.value_from_object(instance) if isinstance(f,models.BooleanField): if current_value: current_value=1 else: current_value=0 except ValueError: pass elif f.has_default and not (f.default==models.fields.NOT_PROVIDED): current_value=f.default if read_only or f.name in lock_fields or \ (f.primary_key and current_value): #被锁定的字段不能被修改,主键不能被修改 formfield = widgets.form_field_readonly(f, initial=current_value) else: widget=None if f.name in default_widgets: widget=default_widgets[f.name] elif f.__class__ in default_widgets: widget=default_widgets[f.__class__] if widget: formfield = formfield_callback(f, initial=current_value, widget=widget) else: formfield = formfield_callback(f, initial=current_value) if formfield: widgets.check_limit(f, model, formfield, instance) if formfield: field_list.append((f.name, formfield)) base_fields = SortedDict(field_list) return type(opts.app_label+"_"+model.__name__+'_edit', (form,), {'base_fields': base_fields, '_model': model, 'save': make_instance_save(instance or model(), fields, 'created'), 'data_valid':make_instance_data_valid(instance or model(), fields), })(post)
def form_field(f, readonly=False, **kwargs): if readonly: return form_field_readonly(f, **kwargs) return form_field_default(f, **kwargs)
def _form_for_model(model, instance=None, form=forms.BaseForm, fields=None, post=None, formfield_callback=widgets.form_field, lock_fields=[], read_only=False, level_type=0): if hasattr(model.Admin, "form_class"): if model.Admin.form_class: return model.Admin.form_class(post, instance=instance) opts = model._meta field_list = [] default_widgets = {} if hasattr(model.Admin, "default_widgets"): default_widgets = model.Admin.default_widgets if callable(default_widgets): default_widgets(instance) for f in opts.fields + opts.many_to_many: if not f.editable: continue if fields and not f.name in fields: continue # if post and not f.name in post.keys(): # continue current_value = None #print "////f=",f if instance: try: current_value = f.value_from_object(instance) if isinstance(f, models.BooleanField): if current_value: current_value = 1 else: current_value = 0 except ValueError: pass elif f.has_default and not (f.default == models.fields.NOT_PROVIDED): current_value = f.default if read_only or f.name in lock_fields or \ (f.primary_key and current_value): #被锁定的字段不能被修改,主键不能被修改 formfield = widgets.form_field_readonly(f, initial=current_value) else: widget = None if f.name in default_widgets: widget = default_widgets[f.name] elif f.__class__ in default_widgets: widget = default_widgets[f.__class__] if widget: formfield = formfield_callback(f, initial=current_value, widget=widget) else: formfield = formfield_callback(f, initial=current_value) if formfield: widgets.check_limit(f, model, formfield, instance, level_type) if formfield: field_list.append((f.name, formfield)) base_fields = SortedDict(field_list) return type( opts.app_label + "_" + model.__name__ + '_edit', (form, ), { 'base_fields': base_fields, '_model': model, 'save': make_instance_save(instance or model(), fields, 'created'), 'data_valid': make_instance_data_valid(instance or model(), fields), })(post)