Login

Alias Field

Author:
s29
Posted:
December 10, 2014
Language:
Python
Version:
Not specified
Score:
0 (after 0 ratings)

A "fake" model field which is an alias to another underlying database field.

Mirrors everything including queryset lookups, instance attributes, even if the field has been used on the model already (this isn't possible just by using db_column).

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
class AliasField(models.Field):
    def contribute_to_class(self, cls, name, virtual_only=False):
        super(AliasField, self).contribute_to_class(cls, name, virtual_only=True)
        setattr(cls, name, self)
    
    def __get__(self, instance, instance_type=None):
        return getattr(instance, self.db_column)


class Order(models.Model):
    """
    The main order model
    """
    number = AliasField(db_column='id')

More like this

  1. get_object_or_none by azwdevops 3 months, 2 weeks ago
  2. Mask sensitive data from logger by agusmakmun 5 months, 1 week ago
  3. Template tag - list punctuation for a list of items by shapiromatron 1 year, 7 months ago
  4. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 1 year, 7 months ago
  5. Serializer factory with Django Rest Framework by julio 2 years, 2 months ago

Comments

Please login first before commenting.