I have a model with a 'User' foreign key.
class Domain(models.Model):
.
owner = models.ForeignKey(User)
I have a requirement to only allow superuers to change the 'owner'
field.
I can do this by creating two model forms and excluding the 'owner'
field from one:
class DomainForm_superuser(ModelForm):
class Meta:
model = Domain
class DomainForm(ModelForm):
class Meta:
model = Domain
exclude = ('owner',)
and then in my view I check if the current user is a superuser
@login_required()
def update_domain(request, domain_id):
d = Domain.objects.get(id=domain_id)
if request.method == 'POST':
if request.user.is_superuser:
form = DomainForm_superuser(data=request.POST,
instance=d)
else:
form = DomainForm(data=request.POST,
instance=d)
if form.is_valid():
form.save()
return HttpResponseRedirect("/")
else:
if request.user.is_superuser:
form = DomainForm_superuser(instance=d)
else:
form = DomainForm(instance=d)
return render_to_response('domain_form.html', {"form": form},
context_instance=RequestContext(request))
This works but can anyone suggest a better/more efficient way of doing
this?
Paddy
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---