Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use an UpdateView to update a Django Model?

Tags:

I'm trying to update a model in Django using the class-based generic view UpdateView.

I read the page Updating User model in Django with class based UpdateView to try and get me started, but I'm getting an error 'WSGIRequest' object has no attribute 'id'

I'm a fresh face to Django, so please be forgiving if I'm doing something stupid.

//urls.py

url(r'^portfolios/update/(?P<id>\d+)/$',PortfoliosUpdateView.as_view()), 

//views.py

class PortfoliosUpdateView(UpdateView):     form_class = PortfoliosCreateForm     model = Portfolios     template_name = 'portfolios/create.html'      def get(self, request, **kwargs):         self.object = Portfolios.objects.get(id=self.request.id)         form_class = self.get_form_class()         form = self.get_form(form_class)         context = self.get_context_data(object=self.object, form=form)         return self.render_to_response(context)      def get_object(self, queryset=None):         obj = Portfolios.objects.get(id=self.request.id)         return obj 

It's mostly just a modified version of the code originally posted, but I thought it'd work. I know that I'm trying to retrieve the id passed as a GET parameter, but that doesn't seem to come through in the request variable. Am I going about this the wrong way?

Thanks

Edit: I think I fixed it, but this may be wrong: I changed the lines

self.object = Portfolios.objects.get(id=self.request.id) obj = Portfolios.objects.get(id=self.request.id) 

to

self.object = Portfolios.objects.get(id=self.kwargs['id']) obj = Portfolios.objects.get(id=self.kwargs['id']) 

I could be wrong.

like image 994
xyzjace Avatar asked Feb 14 '12 04:02

xyzjace


People also ask

How do I update model fields in Django?

Use update_fields in save() If you would like to explicitly mention only those columns that you want to be updated, you can do so using the update_fields parameter while calling the save() method. You can also choose to update multiple columns by passing more field names in the update_fields list.

What is Django UpdateView?

UpdateView is a view in Django which is used to update any model data from frontend. It is a built-in view that can be easily applied. It acts like an Admin page in updating the view. In this article, we will take an example and demonstrate how to use UpdateView in Django.

What does CreateView do in Django?

CreateView. A view that displays a form for creating an object, redisplaying the form with validation errors (if there are any) and saving the object. This view inherits methods and attributes from the following views: django.


1 Answers

It should be:

def get_object(self, queryset=None):     obj = Portfolios.objects.get(id=self.kwargs['id'])     return obj 

Look at class based generic view dispatch explains that keyword arguments are assigned to self.kwargs.:

def dispatch(self, request, *args, **kwargs):     # Try to dispatch to the right method; if a method doesn't exist,     # defer to the error handler. Also defer to the error handler if the     # request method isn't on the approved list.     if request.method.lower() in self.http_method_names:         handler = getattr(self, request.method.lower(), self.http_method_not_allowed)     else:         handler = self.http_method_not_allowed     self.request = request     self.args = args     self.kwargs = kwargs     return handler(request, *args, **kwargs) 
like image 113
bmihelac Avatar answered Oct 02 '22 18:10

bmihelac