Using generic create_update views with non-editable fields in a model

2009-06-04 Thread Vladimir Ionescu

Hi all,

I am new to Django, Python and web development in general. I've been
playing with Django for a couple of days and I like it. I am writing
an application that will allow multiple users to create, update and
delete objects. At this point I would like to limit users' access to
their own objects. My object model has this field to determin the
owner:

user = models.ForeignKey(User, editable = False)

Now when I tried using the generic create view, I got errors because
obviously the user field was not being filled in anywhere. Also, I
wanted to make update and delete only work if request.user is the same
as object.user.

I created a custom ModelForm for my objects:

class MyObjectForm(ModelForm):
class Meta:
model = MyObject
exclude = ('user', )
userid = None
def save(self, commit=True):
m = super(MyObjectForm, self).save(commit=False)
m.user = User.objects.get(pk = self.Meta.userid)
if commit:
m.save()
# the next call does not work... why? I must have
misunderstood the docs
#super(MyObjectForm, self).save_m2m()
return m

This overrides the save() method and tries to set the userid as the
one in the Meta class

Next I created wrappers for the generic views. The my_object_create
one construct a new ModelForm from the MyObjectForm and sets the
Meta.userid to the request.user.id. The update and delete take the
object id and check that object.user == request.user

def my_object_create(request, **kwargs):
class_name = 'MyObjectForm_' + request.user.username
class Meta:
model = MyObject
exclude = ('user', )
userid = request.user.id
kwargs['form_class'] = ModelFormMetaclass(str(class_name),
(MyObjectForm,), {'Meta': Meta})
return create_object(request, **kwargs)

def my_object_update(request, **kwargs):
try:
id = kwargs['object_id']
except KeyError:
raise Http404("No id specified")
if MyObject.objects.get(pk=id).user != request.user:
raise Http404("No object found")
return update_object(request, **kwargs)

def my_object_delete(request, **kwargs):
try:
id = kwargs['object_id']
except KeyError:
raise Http404("No id specified")
if MyObject.objects.get(pk=id).user != request.user:
raise Http404("No object found")
return delete_object(request, **kwargs)

Now I am not very happy with this because it is not generic (if I want
a new object with different non-editable fields I a new form and new
wrappers), and because in the update and delete case I do the object
lookup twice (once in the wrapper to check for permission, and once in
the generic view). If I were to work on a real thing I would probably
give up on the generic views.

I've read that a new object-based generic view system is coming and it
would solve this. Are there any other solutions? What do you think?

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



Re: decoupling urlconfs

2009-06-04 Thread Vladimir Ionescu

In your main urls.py you only have one regex matchin anything starting
with polls, so "/" does not match anything. If you type 
http://127.0.0.1:8000/polls/
you should see your index.

On Jun 4, 6:45 pm, Ross  wrote:
> I've been working my way through the Django tutorial and everything
> has gone fine until I came across the part near the end of section 3
> about decoupling the urlconfs. I did as follows: I copied urls.py into
> my polls directory (C:\mysite\polls\urls.py). It now looks like this:
>
> from django.conf.urls.defaults import *
>
> from django.contrib import admin
> admin.autodiscover()
>
> urlpatterns = patterns('mysite.polls.views',
>     (r'^$', 'index'),
>     (r'^(?P\d+)/$', 'detail'),
>     (r'^(?P\d+)/results/$', 'results'),
>     (r'^(?P\d+)/vote/$', 'vote'),
> )
>
> I then changed the original urls.py (C:\mysite\urls.py) to look like
> this:
>
> from django.conf.urls.defaults import *
>
> from django.contrib import admin
> admin.autodiscover()
>
> urlpatterns = patterns('',
>     (r'^polls/', include('mysite.polls.urls')),
> )
>
> Unfortunately, now when I runserver and open my localhost 
> athttp://127.0.0.1:8000/, I get a 404 page not found message. What
> happened?
--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---