Opened 19 years ago

Closed 19 years ago

Last modified 18 years ago

#420 closed defect (fixed)

[patch] exclude_fields in AddManipulator and ChangeManipulator

Reported by: maurycypw@… Owned by: Adrian Holovaty
Component: Metasystem Version:
Severity: normal Keywords: new-admin
Cc: wojtek@… Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

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))

Change History (7)

comment:1 by anonymous, 19 years ago

Summary: [patch] exclude_fields in Manipulator[patch] exclude_fields in AddManipulator and ChangeManipulator

comment:2 by hugo <gb@…>, 19 years ago

#445 is a possible duplicate of this (and would at least be partially fixed by this patch)

comment:3 by robert@…, 19 years ago

Really I'd like a solution which would work for generic views : ie the validation of fields should be suppressable from the template.

eg something like {{form.field_name.suppress}}

The problem is that this is normally processed on the request, rather than the response.
So it would be possible to add another bit during your POST handling section, which rerenders the template, and then looks at the FormWrapper variable to see which fields are suppressed, and informs the manipulator. It might be possible to do this more efficiently, eg have a special tag like {{ suppress form.field_name }} , and then just have a visitor look for those specifically rather than render the whole template again.

I'll give this a go.

The other solution for generic views is yet another keyword arg which specifies the manipulator ( or maybe just the exclude list? )

comment:4 by robert@…, 19 years ago

So eventually I did something kind of similar to this in the new_admin branch , r773.
A manipulator can have a new keyword arg, follow. Its used to specify which fields and related objects to deal with.

Eg

follow = { 'name': False,

'accounts': {

'balance': True,

}

}

manipulator = customer.ChangeManipulator(id, follow=follow);

This would load/ allow modification of all the fields in the default except 'name', and would additionally fill the manipulator with the inline editing fields for the related object accounts ( regardless of its inline_edit argument), specifically enabling the field balance in that inline object.

This is a much more complex use than normal, but it shows why I chose this argument format: it is easy to express the hierarchy that the manipulator will follow, however complex.

comment:5 by anonymous, 19 years ago

Keywords: new-admin added

comment:6 by rjwittams, 19 years ago

Resolution: fixed
Status: newclosed

Fixed in new-admin merge

comment:7 by Go, 18 years ago

Type: enhancementdefect
Note: See TracTickets for help on using tickets.
Back to Top