Jorge Gajon ha scritto:

> On 10/28/06, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
> > 1) {{form.teacher}} doesn't render the actual teacher assigned to that
> > course but just the first in the list of teachers.
> >
>
> I'm guessing here since I don't have open any project to test it on,
> but I suspect that is because you are initializing your form data with
> this:
>
>    new_data = dict(code=course.code, name=course.name,
> description=course.description, teacher=course.teacher,
> assistant=course.assistant)
>
> which makes new_data['teacher'] contain a teacher object when you
> actually only need a value. You could change that to:
> teacher=str(course.teacher.id)

You're right, this works the way i wanted it!

> Another thing, maybe you are doing more things that you didn't
> included in the pasted code, but I don't see any need for a custom
> manipulator there, and I would suggest you to use the default
> generated manipulators.

Yes, it wasn't anymore of an exercise here but i do need custom
manipulators for other models in my app.

> Your code could be more simpler, like this:
>
> def course_edit(request, course_id):
>     try:
>         manipulator = Course.ChangeManipulator(course_id)
>     except:
>         raise Http404
>
>     course = manipulator.original_object
>     new_data = {}
>     errors = {}
>
>     if request.POST:
>         new_data = request.POST.copy()
>         errors = manipulator.get_validation_errors(new_data)
>         if not errors:
>             manipulator.do_html2python(new_data)
>             manipulator.save(new_data)
>
>             return HttpResponseRedirect("/course_edit/%s" % course_id)
>     else:
>         new_data = manipulator.flatten_data()
>
>     form = forms.FormWrapper(manipulator, new_data, errors)
>     return render_to_response("course.html", {'form'=form})
>
>
> > 2) In the manipulator save() method i have to refer to model fields
> > with course.teacher_id. Shouldn't i be able to just do course.teacher
> > without instantiating a teacher object?
> >
> No, course.teacher must be a Teacher object. And since you are using a
> custom manipulator, you'll need to fetch that object in the save()
> method.
>
> Or, as you already noted, you can use course.teacher_id and assign it
> a valid id.

I understand now.

Also I found i could just add a custom validator:

class UpdateBuildingManipulator(forms.Manipulator):
    def __init__(self):
        self.fields = (forms.TextField("building_name",
is_required=True),
                      forms.TextField("building_code",
is_required=True, validator_list=[self.unique_code]),
        )

    def unique_code(self, field_data, all_data):
        try:
            Building.objects.get(building_code=field_data)
            raise ValidationError, gettext("A building with that code
already exists")
        except Building.DoesNotExist: pass

    def save(self, form_data):
        building = Building(building_name=form_data["building_name"],
building_code=form_data["building_code"])
        building.save()

Thank you very much for looking at this.

Lorenzo


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---

Reply via email to