Django proxies

I am trying to figure out how to use proxy classes in Django. I want to get a request where each object belongs to the proxy class of a regular superclass so that I can run my own subclassed methods with the same name, and my controller logic does not need to know or care about what kind of proxy server model it works with . One thing I don’t want to do is to store information in multiple tables, because I want to have uniform identifiers to simplify reference / management.

I am new to django / python, so I would be happy to hear alternative ways to achieve what I'm trying to do.

Here is what I have:

TYPES = (
    ('aol','AOL'),
    ('yhoo','Yahoo'),
)

class SuperConnect(models.Model):
  name = models.CharField(max_length=90)
  type = models.CharField(max_length=45, choices = TYPES)
  connection_string = models.TextField(null=True)

class ConnectAOL(SuperConnect):
  class Meta:
    proxy = True

  def connect(self):
     conn_options = self.deconstruct_constring()
     # do special stuff to connect to AOL

  def deconstruct_constring(self):
     return pickle.loads(self.connection_string)

class ConnectYahoo(SuperConnect):
  class Meta:
    proxy = True

  def connect(self):
     conn_options = self.deconstruct_constring()
     # do special stuff to connect to Yahoo

  def deconstruct_constring(self):
     return pickle.loads(self.connection_string)

Now I want to do the following:

connections = SuperConnect.objects.all()

for connection in connections:
  connection.connect()
  connection.dostuff

, , , , , ...

-, , :) :

class MixedQuerySet(QuerySet):
    def __getitem__(self, k):
        item = super(MixedQuerySet, self).__getitem__(k)
        if item.atype == 'aol':
            yield(ConnectAOL.objects.get(id=item.id))
        elif item.atype == 'yhoo':
            yield(ConnectYahoo.objects.get(id=item.id))
        else:
            raise NotImplementedError

    def __iter__(self):
        for item in super(MixedQuerySet, self).__iter__():
            if item.atype == 'aol':
                yield(ConnectAOL.objects.get(id=item.id))
            elif item.atype == 'yhoo':
                yield(ConnectYahoo.objects.get(id=item.id))
            else:
                raise NotImplementedError

class MixManager(models.Manager):
    def get_query_set(self):
        return MixedQuerySet(self.model)

TYPES = (
    ('aol','AOL'),
    ('yhoo','Yahoo'),
)

class SuperConnect(models.Model):
  name = models.CharField(max_length=90)
  atype = models.CharField(max_length=45, choices = TYPES)
  connection_string = models.TextField(null=True)
  objects = MixManager()

class ConnectAOL(SuperConnect):
  class Meta:
    proxy = True

  def connect(self):
     conn_options = self.deconstruct_constring()
     # do special stuff to connect to AOL

  def deconstruct_constring(self):
     return pickle.loads(self.connection_string)

class ConnectYahoo(SuperConnect):
  class Meta:
    proxy = True

  def connect(self):
     conn_options = self.deconstruct_constring()
     # do special stuff to connect to Yahoo

  def deconstruct_constring(self):
     return pickle.loads(self.connection_string)
+3
2

. - :

def connect(self):
    return getattr(self, "connect_%s" % self.type)()

def connect_aol(self):
    pass # AOL stuff

def connect_yahoo(self):
    pass # Yahoo! stuff

type, ( ) , -.

, , .

0

, , SQL- SQL in = (id1, id2). - , SQL-.

SuperConnect SuperConnect.__init__, __class__:

class SuperConnect(models.Model):
    name = models.CharField(max_length=90)
    type = models.CharField(max_length=45, choices = TYPES)
    connection_string = models.TextField(null=True)

    def __init__(self, *args, **kwargs):
        super(SuperConnect, self).__init__(*args, **kwargs)
        if self.type == 'aol':
            self.__class__ = ConnectAOL
        elif self.type == 'yahoo':
            self.__class__ = ConnectYahoo

, SuperConnect.

+1

Source: https://habr.com/ru/post/1769063/


All Articles