Thursday, May 28, 2015

Get newly created object id and then save the object in different DB?

In Django function based view I can get newly created object id and again save that object to different database in this way :

data = Password(username=username, password=encrypted_pass,                                    note=encrypted_note,added_at=timezone.now())   data.save()   Id = data.id   data.pk = None   data.save(using='backup')

But how can I do the same thing in Class based view ? Here is my view :

class AddData(LoggedInMixin, CreateView):      model = Password      success_url = '/dashboard'      template_name = 'forms/add.html'      fields = ['site_url', 'username', 'email', 'password', 'note']        def form_valid(self, form):          form.instance.added_at = timezone.now()          return super(AddData, self).form_valid(form)        def form_invalid(self, form):          return self.render_to_response(self.get_context_data(form=form))

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/042e6c2a-686f-4c00-89d7-7215589e22cf%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment