2014-02-10 5 views
0

Я использую стороннее приложение Django, то есть django-geo. Одна из его моделей, то есть LocationType, не имеет метода __unicode__, и я хотел бы добавить это. См ниже текущей декларации модели третьей стороны:Как переопределить сторонние библиотеки в django

class LocationType(models.Model): 
"""Type of the location (city, village, place, locality, neighbourhood, etc.) 
This is not intended to contain anything inside it. 
""" 
uuid = UUIDField(auto=True, blank=False, version=1, help_text=_('unique id'), default="") 
description = models.CharField(unique=True, max_length=100) 
objects = LocationTypeManager() 

class Meta: 
    verbose_name_plural = _('Location Types') 
    verbose_name = _('Location Type') 
    app_label = 'geo' 

def natural_key(self): 
    return (self.uuid.hex,) 

Как вы можете видеть, выше класс не имеет методы __unicode__.

И ниже пользовательский код, который я хочу добавить к моему заявлению:

LocationType.__unicode__ = lambda location_type: location_type.description 

В обычном приложении Django, где бы я добавить код выше обезьяна патч? Является ли это в любом приложении, или мне, возможно, придется создать другое приложение, чтобы разместить главный код?

ответ

0

Добавить прокси = True для YOUT класса (https://docs.djangoproject.com/en/dev/topics/db/models/#multi-table-inheritance)

class LocationType(models.Model): 
"""Type of the location (city, village, place, locality, neighbourhood, etc.) 
This is not intended to contain anything inside it. 
""" 
uuid = UUIDField(auto=True, blank=False, version=1, help_text=_('unique id'), default="") 
description = models.CharField(unique=True, max_length=100) 
objects = LocationTypeManager() 

class Meta: 
    verbose_name_plural = _('Location Types') 
    verbose_name = _('Location Type') 
    app_label = 'geo' 

def natural_key(self): 
    return (self.uuid.hex,) 

class MyLocationType(LocationType): 
    class Meta: 
     proxy = True 

    def __unicode__(self): 
     return 'whatever' 
Смежные вопросы