Into trunk merged queryset-refactor branch, that makes it easy to inherit django-models. It has been known to all. But inheritance is not the only alternative, it can be also used Monkey patching.
The main idea is that Monkey patching also lets you add not only the fields, but also the functions.
For example, so will be added in the model User the field OpenID, and the additional function, that returns the nickname, if there is no name
from django.contrib.auth.models import User

User.add_to_class('openid', models.CharField(max_length=250,blank=True))

def get_user_name(self):
    if self.first_name or self.last_name:
        return self.first_name + " " + self.last_name
    return self.username

User.add_to_class("get_user_name",get_user_name)
The main benefits of the method:
additional tables are not created
in the work with the model will be used standard instruments of django

13 Votes | Average: 4.4 out of 513 Votes | Average: 4.4 out of 513 Votes | Average: 4.4 out of 513 Votes | Average: 4.4 out of 513 Votes | Average: 4.4 out of 5 (13 votes, average: 4.4 out of 5)
Loading ... Loading ...

Top Posts: