This patch adds exclude_fields param to the AddManipulator? and ChangeManipulator?, which points fields to omit during validation.
Currently when you create new form for creating or updating an object, that doesn't inherit few fields from its Model, you have to create your own custom Manipulator:
class Entry(meta.Model):
headline = meta.CharField('headline', maxlength=100)
content = meta.TextField('content')
submit_date = meta.DateTimeFied('date/time submitted', auto_now_add=True)
class META:
module_name = 'entries'
class EntryManipulator(formfields.Manipulator):
def __init__(self, blog, user):
self.blog, self.user = blog, user
self.fields = (
formfields.CharField(field_name='headline', is_rqeuired=True,
length=30, maxlength=100),
formfields.TextField(field_name='content', is_required=True),
)
def save(self, new_data):
entry = entries.Entry()
entry.headline = new_data['headline']
entry.content = new_data['content']
entry.save()
def create_entry(request):
"Create new entry, without validation."
manipulator = EntryManipulator()
new_data = request.POST.copy()
manipulator.do_html2python(new_data)
manipulator.save(new_data)
With exclude_fields, it can be written shorter:
class Entry(meta.Model):
headline = meta.CharField('headline', maxlength=100)
content = meta.TextField('content')
submit_date = meta.DateTimeFied('date/time submitted', auto_now_add=True)
class META:
module_name = 'entries'
def create_entry(request):
"Create new entry, without validation."
manipulator = entries.AddManipulator(exclude_fields=['submit_date'])
new_data = request.POST.copy()
manipulator.do_html2python(new_data)
manipulator.save(new_data)
Patch:
--- django_src/django/core/meta/__init__.py 2005-08-26 15:03:29.000000000 +0
200
+++ django_src.exclude_fields/django/core/meta/__init__.py 2005-08-26 16:35
:25.000000000 +0200
@@ -1396,7 +1396,7 @@
setattr(man, k, v)
return man
-def manipulator_init(opts, add, change, self, obj_key=None):
+def manipulator_init(opts, add, change, self, obj_key=None, exclude_fields=[]):
if change:
assert obj_key is not None, "ChangeManipulator.__init__() must be passe
d obj_key parameter."
self.obj_key = obj_key
@@ -1421,7 +1421,7 @@
raise
self.fields = []
for f in opts.fields + opts.many_to_many:
- if f.editable and not (f.primary_key and change) and (not f.rel or not
f.rel.edit_inline):
+ if f.editable and not (f.primary_key and change) and (not f.rel or not
f.rel.edit_inline) and not (f.name in exclude_fields):
self.fields.extend(f.get_manipulator_fields(opts, self, change))
# Add fields for related objects.
@@ -1436,7 +1436,7 @@
else:
count = rel_field.rel.num_in_admin
for f in rel_opts.fields + rel_opts.many_to_many:
- if f.editable and f != rel_field and (not f.primary_key or (f.prima
ry_key and change)):
+ if f.editable and f != rel_field and (not f.primary_key or (f.prima
ry_key and change)) and not (f.name in exclude_fields):
for i in range(count):
self.fields.extend(f.get_manipulator_fields(rel_opts, self,
change, name_prefix='%s.%d.' % (rel_opts.object_name.lower(), i), rel=True))