This seems hacky but it's something I've come up with so far that
seems to do what I need it to.  I have some issues with permissiosn to
display still but I'm working on it.  My veiw method is as follows:

@login_required
@has_perm_or_owner_of('can change release', Release)
def update_release(request, project_fk, id):
    '''
    Method for updating releases based on user group.

    This builds forms for display based on user permissions and roles
and loads
    partial releae editing forms as appropriate.  The actual
validation and
    submission happens through a meta release form object and this
saves me from
    having to explicitly define undeclaired fields.  This is useful
since those
    fields will changed based on user role.
    '''
    release = get_object_or_404(Release, pk=id)
    forms = {}
    if request.method == 'POST':
        # Bind data to an all encompasing model form.
        baseform = ReleaseForm(request.POST, instance=release)

        # If the data is valid then save it.
        if baseform.is_valid():
            form_instance = baseform.save(commit=False)
            form_instance.save()
            return HttpResponseRedirect('/project/%s/release/%s/' %
(project_fk, release.id))

        else:
            # If the data is not valid then return the individual
forms and errors
            if request.user.has_perms('project.can_update_release') |
request.user.id == release.owner.id:
                forms['basic'] = ReleaseBasicForm(request.POST,
instance=release)
            if request.user.has_perms('project.can_update_plan'):
                forms['planning'] = ReleasePlanningForm(request.POST,
instance=release)
            if request.user.has_perms
('project.can_update_development'):
                forms['development'] = ReleaseDevelopmentForm
(request.POST, instance=release)
    else:
        baseform = ReleaseForm(instance=release)
        if request.user.has_perm('project.can_change_release') |
request.user.id == release.owner.id:
            forms['basic'] = ReleaseBasicForm(instance=release)
        if request.user.has_perm('project.can_update_plan'):
            forms['planning'] = ReleasePlanningForm(instance=release)
        if request.user.has_perm('project.can_update_development'):
            forms['development'] = ReleaseDevelopmentForm
(instance=release)

    return render_with_context(request, 'project/releaseedit.xhtml', {
                               'baseform': baseform,
                               'forms': forms,
                               'action': '%s/release/%s/edit/' %
(release.project_fk.id, release.id),
                               'button': 'Update Release',
                               'title': 'Edit Release (%s)' %
release.project_fk.title,
                               })

On Jul 16, 4:05 pm, Streamweaver <streamwea...@gmail.com> wrote:
> I have a model with a number of attributes (some included as example
> below):
>
> class Release(models.Model):
>     project_fk = models.ForeignKey(Project)
>     title = models.CharField(max_length=30)
>     summary = models.TextField()
>     owner = models.ForeignKey(User)
>     planning_status = models.CharField(max_length=1,
> choices=PS_CHOICES, default=PS_CHOICES[0][0])
>     development_status = models.CharField(max_length=1,
> choices=DEV_CHOICES, default=DEV_CHOICES[0][0])
>     point_estimate = models.IntegerField(choices=ESTIMATE_CHOICES,
> default=0)
>     staffing_level = models.FloatField(default=0,
> choices=STAFF_CHOICES)
>     internal_priority = models.IntegerField(default=0)
>     created_on = models.DateField(auto_now=False, auto_now_add=True)
>     last_updated = models.DateField(auto_now=True, auto_now_add=True)
>
> I want a varying number of these fields to appear in an edit form
> depending on the group a user belongs to.
>
> I was thinking I would have seperat form objects and specify the
> fields I need in each return, returning them in the view after testing
> for the proper user group.
>
> class ReleaseBasicForm(forms.ModelForm):
>     """Form for basic release information."""
>     class Meta:
>         model = Release
>         fields = (
>             'title', 'summary', 'owner'
>         )
>
> class ReleasePlanningForm(forms.ModelForm):
>     """Form for release planning details."""
>     class Meta:
>         model = Release
>         fields = (
>             'planning_status', 'staffing_level'
>         )
>
> class ReleaseDevelopmentForm(forms.ModelForm):
>     """Form for release development details."""
>     class Meta:
>         model = Release
>         fields = (
>             'development_status', 'point_estimate'
>         )
>
> I'm having trouble though understanding how I would process this form
> data.  In a normal model form I'd have to save a form with
> 'commit=false' and then add back in whatever fields I excluded or it
> will save those as null.  Since what fields will appear will change by
> user role though I'm not sure how best to approach this.
>
> Any  help would be appreciated.
--~--~---------~--~----~------------~-------~--~----~
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 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to