Wednesday, July 4, 2012

Re: problem with django model method save()

I don't see why wouldn't your model's .save() work, maybe we'd need to see the model code, but I know you probably want to read this:

https://docs.djangoproject.com/en/dev/topics/forms/modelforms/

once you've implemented a ModelForm, you'd pretty much would just do

form = RoomForm(request.POST)
if form.is_valid():
  form.save()

On Wed, Jul 4, 2012 at 3:58 PM, rafiee.nima <rafiee.nima@gmail.com> wrote:
> Hi Im some how new to django . I write a view to handle ajax request
> but I find out that save() method dose not save model instance in to the
> database
> here is my code
>
> def add_room(request):
>     context={}
>     status=''
>     if request.is_ajax:
>         if request.POST:
>             hotel_instance=Hotel.objects.get(id=request.POST['hotel'])
>             room_id=int(request.POST['id'])
>             if room_id > 0 :
>                 room=HotelRoom.objects.get(id=request.POST['id'])
>                 room.hotel=hotel_instance
>                 room.number=request.POST['number']
>                 room.bed_count=request.POST['bed_count']
>                 room.ground_sleep=request.POST['ground_sleep']
>                 room.view=request.POST['view']
>                 room.reserved=request.POST['reserved']
>                 room.tv=request.POST['tv'].capitalize()
>                 room.phone=request.POST['phone']
>                 room.refrigerator=request.POST['refrigerator']
>                 room.air_condition=request.POST['air_condition']
>                 room.toilet=request.POST['toilet']
>                 room.creator=request.user
>                 room.save()
>                 room_json=model_to_dict(room)
>                 status="room successfully updated"
>             else:
>                 room = HotelRoom(
>                     hotel=hotel_instance,
>                     number=request.POST['number'],
>                     bed_count=request.POST['bed_count'],
>                     ground_sleep=request.POST['ground_sleep'],
>                     view=request.POST['view'],
>                     reserved=request.POST['reserved'],
>                     tv=request.POST['tv'],
>                     phone=request.POST['phone'],
>                     refrigerator=request.POST['refrigerator'],
>                     air_condition=request.POST['air_condition'],
>                     toilet=request.POST['toilet'],
>                     creator=request.user )
>                 room.save()
>                 status="new room successfully added "
>         raw_data={'status':status,'data':room_json}
>         data=simplejson.dumps(raw_data)
>         return HttpResponse(data, mimetype="application/json")
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/django-users/-/Q3kHTKx-BX4J.
> To post to this group, send email to django-users@googlegroups.com.
> To unsubscribe from this group, send email to
> django-users+unsubscribe@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/django-users?hl=en.



--
"The whole of Japan is pure invention. There is no such country, there are no such people" --Oscar Wilde

|_|0|_|
|_|_|0|
|0|0|0|

(\__/)
(='.'=)This is Bunny. Copy and paste bunny
(")_(") to help him gain world domination.

--
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+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment