Retrieving a key from a new object created from create_object using Django generic views

My code looks like this:

def add_cart(request):
    return create_object(request, form_class=CartForm,
    post_save_redirect=reverse('test.views.show_cart',
                               kwargs=dict(object_id='%(key)s')))

      

Ideally I would like it to look like this:

def add_cart(request):
    newobject = create_object(request, form_class=CartForm,
    post_save_redirect=reverse('test.views.show_cart',
                               kwargs=dict(object_id='%(key)s'))) 
    request.session['cartid'] = newobject.key
    return newobject

      

How do I get a key from a newly created object?

I am using google-app-engine-patch with all the latest versions.

+1


a source to share


2 answers


In Django, you can use

newobject.id

      

or



newobject.pk

      

I recommend using .pk.

Remember that newobject.pk is not always the same as newobject.id.

0


a source


Currently, generic views involving create_object do not provide hooks for additional processing on the created object. So if you want to do this, you have to collapse your own view, which is really not very difficult.



When Django 1.2 is running, we should get class-based generic views, which will make it much easier to use the extra bits of functionality.

0


a source







All Articles