daily pastebin goal
59%
SHARE
TWEET

Untitled

a guest Jun 23rd, 2018 77 Never
Upgrade to PRO!
ENDING IN00days00hours00mins00secs
  1. class ModelA(models.Model):
  2.     class Meta:
  3.         permisssions =(('view_modela','Can view Model A'),)  
  4.  
  5. class ModelB(models.Model):
  6.     class Meta:
  7.         permisssions =(('view_modelb','Can view Model B'),)
  8.    
  9. class CustomModel(models.Model):
  10.     def __init__(self, *args, **kwargs):
  11.         self._meta.permissions.append(('view_'+self._meta.module_name, u'Can view %s' % self._meta.verbose_name))
  12.         super(CustomModel,self).__init__(*args, **kwargs)
  13.  
  14. class ModelA(CustomModel):
  15.     ....
  16.  
  17. class ModelB(CustomModel):
  18.     ...
  19.    
  20. from django.db.models.base import ModelBase
  21.  
  22. class CustomModelMetaClass(ModelBase):
  23.  
  24.     def __new__(cls, name, bases, attrs):
  25.         klas = super(CustomModelMetaClass, cls).__new__(cls, name, bases, attrs)
  26.         klas._meta.permissions.append(('view_' + klas._meta.module_name, u'Can view %s' % klas._meta.verbose_name))
  27.  
  28.         return klas
  29.  
  30. class ModelA(models.Model):
  31.  
  32.     __metaclass__ = CustomModelMetaClass
  33.  
  34.     test = models.CharField(max_length=5)
  35.    
  36. #create a custom manager
  37. class DynTableNameManager(models.Manager):
  38.     #overwrite all() (example)
  39.     #provide table_name
  40.     def all(self, table_name):
  41.         from django.db import connection
  42.         cursor = connection.cursor()
  43.         cursor.execute("""
  44.             SELECT id, name
  45.             FROM %s
  46.             """ % table_name)
  47.         result_list = []
  48.         for row in cursor.fetchall():
  49.             p = self.model(id=row[0], name=row[1])
  50.             result_list.append(p)
  51.         return result_list
  52.  
  53. #cerate a dummy table
  54. class DummyTable(models.Model):
  55.     name = models.CharField ( max_length = 200 )
  56.     objects = DynTableNameManager()
  57.    
  58. f = DummyTable.objects.all('my_table_name')
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top