Here are the examples of the python api django.db.router.db_for_write taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
74 Examples
3
View Complete Implementation : delete.py
Copyright Apache License 2.0
Author : BeanWei
Copyright Apache License 2.0
Author : BeanWei
def init_request(self, object_id, *args, **kwargs):
"The 'delete' admin view for this model."
self.obj = self.get_object(unquote(object_id))
if not self.has_delete_permission(self.obj):
raise PermissionDenied
if self.obj is None:
raise Http404(_('%(name)s object with primary key %(key)r does not exist.') % {'name': force_text(self.opts.verbose_name), 'key': escape(object_id)})
using = router.db_for_write(self.model)
# Populate deleted_objects, a data structure of all related objects that
# will also be deleted.
(self.deleted_objects, model_count, self.perms_needed, self.protected) = get_deleted_objects(
[self.obj], self.opts, self.request.user, self.admin_site, using)
3
View Complete Implementation : db.py
Copyright GNU General Public License v2.0
Author : blackye
Copyright GNU General Public License v2.0
Author : blackye
def delete(self, key, version=None):
key = self.make_key(key, version=version)
self.validate_key(key)
db = router.db_for_write(self.cache_model_clast)
table = connections[db].ops.quote_name(self._table)
cursor = connections[db].cursor()
cursor.execute("DELETE FROM %s WHERE cache_key = %%s" % table, [key])
transaction.commit_unless_managed(using=db)
3
View Complete Implementation : base.py
Copyright GNU General Public License v2.0
Author : blackye
Copyright GNU General Public License v2.0
Author : blackye
def delete(self, using=None):
using = using or router.db_for_write(self.__clast__, instance=self)
astert self._get_pk_val() is not None, "%s object can't be deleted because its %s attribute is set to None." % (self._meta.object_name, self._meta.pk.attname)
collector = Collector(using=using)
collector.collect([self])
collector.delete()
3
View Complete Implementation : db.py
Copyright MIT License
Author : bpgc-cte
Copyright MIT License
Author : bpgc-cte
def delete(self, key, version=None):
key = self.make_key(key, version=version)
self.validate_key(key)
db = router.db_for_write(self.cache_model_clast)
connection = connections[db]
table = connection.ops.quote_name(self._table)
with connection.cursor() as cursor:
cursor.execute("DELETE FROM %s WHERE cache_key = %%s" % table, [key])
3
View Complete Implementation : db.py
Copyright MIT License
Author : bpgc-cte
Copyright MIT License
Author : bpgc-cte
def clear(self):
db = router.db_for_write(self.cache_model_clast)
connection = connections[db]
table = connection.ops.quote_name(self._table)
with connection.cursor() as cursor:
cursor.execute('DELETE FROM %s' % table)
3
View Complete Implementation : db.py
Copyright Apache License 2.0
Author : drexly
Copyright Apache License 2.0
Author : drexly
def save(self, must_create=False):
"""
Saves the current session data to the database. If 'must_create' is
True, a database error will be raised if the saving operation doesn't
create a *new* entry (as opposed to possibly updating an existing
entry).
"""
if self.session_key is None:
return self.create()
data = self._get_session(no_load=must_create)
obj = self.create_model_instance(data)
using = router.db_for_write(self.model, instance=obj)
try:
with transaction.atomic(using=using):
obj.save(force_insert=must_create, using=using)
except IntegrityError:
if must_create:
raise CreateError
raise
3
View Complete Implementation : db.py
Copyright Apache License 2.0
Author : edisonlz
Copyright Apache License 2.0
Author : edisonlz
def delete(self, key, version=None):
key = self.make_key(key, version=version)
self.validate_key(key)
db = router.db_for_write(self.cache_model_clast)
table = connections[db].ops.quote_name(self._table)
cursor = connections[db].cursor()
cursor.execute("DELETE FROM %s WHERE cache_key = %%s" % table, [key])
3
View Complete Implementation : models.py
Copyright MIT License
Author : exploreshaifali
Copyright MIT License
Author : exploreshaifali
@transaction.atomic
def delete(self, using=None):
'''
Setting deleted attribtue to new UUID',
also if related objects are on delete cascade:
they will be soft deleted if those related objects have soft deletion
capability
else they will be hard deleted.
'''
using = using or router.db_for_write(self.__clast__, instance=self)
helper = SoftDeleteHelper(using=using, delete_type='soft_delete')
return helper.do_work([self])
3
View Complete Implementation : models.py
Copyright MIT License
Author : exploreshaifali
Copyright MIT License
Author : exploreshaifali
@transaction.atomic
def undelete(self, using=None):
'''setting deleted attribtue to False of current object and all its
related objects if they are on delete cascade'''
using = using or router.db_for_write(self.__clast__, instance=self)
helper = SoftDeleteHelper(using=using, delete_type='soft_undelete')
return helper.do_work([self])
3
View Complete Implementation : models.py
Copyright MIT License
Author : exploreshaifali
Copyright MIT License
Author : exploreshaifali
@transaction.atomic
def hard_delete(self, using=None):
'''setting deleted attribtue to False of current object and all its
related objects if they are on delete cascade'''
using = using or router.db_for_write(self.__clast__, instance=self)
helper = SoftDeleteHelper(using=using, delete_type='hard_delete')
return helper.do_work([self])