2012-08-10 88 views
5

我在寫我的第一個Django應用程序。我有以下的數據庫模型:Django管理員:使用同一模型自動生成多個內聯

class Person(models.Model): 
    first_name   = models.CharField(max_length = 100) 
    last_name   = models.CharField(max_length = 100) 

class InformationType(models.Model): 
    name = models.CharField(max_length = 100) 

class Information(models.Model): 

    person  = models.ForeignKey(Person) 
    info_type = models.ForeignKey(InformationType) 
    info   = models.CharField(max_length = 200) 

我想通過拆分類型信息模型建立在Django管理(類PersonAdmin(的ModelAdmin))的多個內聯和動態做到這一點。另外,我想從用戶界面中隱藏(排除)字段'info_type',並自動填充相應的值。

我可以通過'info_type'過濾'信息'數據動態創建內聯,但在UI中隱藏此字段會使其在保存時爲空。

我該怎麼辦?是否有可能製造隱藏領域?或者我應該存儲'info_type'的值?

我GOOGLE硬,沒有發現任何=)

追加: 確定。我已經修改了 '信息' 類:

class Information(models.Model): 

    def save(self, *args, **kwargs): 
     self.info_type = self.fixed_info_type 
     super(Information, self).save(*args, **kwargs) 

...和maked幾個代理:

class InformationManager(models.Manager): 

    def __init__(self, info_type, *args, **kwargs): 
     self.__info_type = info_type 
     super(InformationManager, self).__init__(*args, **kwargs) 

    def get_query_set(self, *args, **kwargs): 
     return super(self.__class__, self).get_query_set(*args, **kwargs).filter(info_type=self.__info_type) 

class PhoneInformation(Information): 

    fixed_info_type = 'PHONE' 
    objects = InformationManager(fixed_info_type) 
    class Meta: 
     proxy = True 

class EmailInformation(Information): 

    fixed_info_type = 'EMAIL' 
    objects = InformationManager(fixed_info_type) 
    class Meta: 
     proxy = True 

在admin.py:

from contacts.models import Person, PhoneInformation, EmailInformation 
class PersonAdmin(admin.ModelAdmin): 
    __inlines = [] 

    class classproperty(property): 
     def __get__(self, instance, owner): 
      return super(self.__class__, self).fget.__get__(None, owner)() 

    @classproperty  
    @classmethod 
    def inlines(cls): 

     def get_inline(InformationModel): 
      class Inline(admin.TabularInline): 
       model = InformationModel 
       exclude= ['info_type'] 

      return Inline 

     if not cls.__inlines: 
      for InformationModel in [PhoneInformation, EmailInformation]: 
       cls.__inlines.append(get_inline(InformationModel)) 
     return cls.__inlines 

因此,它看起來醜陋和Don」不符合DRY原則。我無法像InlineAdmin一樣創建代理。它每次都得到相同的對象。

回答

1

我找到了解決方案。現在我可以即時創建動態代理模型。 models.py:

class Person(models.Model): 
    first_name   = models.CharField(max_length = 100) 
    last_name   = models.CharField(max_length = 100) 

class InformationType(models.Model): 
    class Meta: 
     ordering = ['order'] 

    name = models.CharField(max_length = 200) 
    order = models.IntegerField() 

class Information(models.Model): 

    person  = models.ForeignKey(Person) 
    info_type = models.ForeignKey(InformationType) 
    value  = models.CharField(max_length = 200) 

    @classmethod  
    def getProxy(cls, info_type): 
     class InformationManager(models.Manager): 
      def get_query_set(self, *args, **kwargs): 
       return super(self.__class__, self).get_query_set(*args, **kwargs).filter(info_type = info_type) 

     def save(self, *args, **kwargs): 
      self.info_type = info_type 
      super(Information, self).save(*args, **kwargs) 

     class Meta: 
      proxy = True 

     return type(
        'Information'+str(info_type.pk), 
        (Information,), 
        { 
         'Meta': Meta, 
         'save': save, 
         '__module__': 'contacts.models', 
         'objects': InformationManager(), 

        } 
        ) 

在admin.py:

class PersonAdmin(admin.ModelAdmin): 

    __inlines = [] 

    class classproperty(property): 
     def __get__(self, instance, owner): 
      return super(self.__class__, self).fget.__get__(None, owner)() 


    @classproperty  
    @classmethod 
    def inlines(cls): 
     def get_inline(info_model): 
      class Inline(admin.TabularInline): 
       model = info_model 
       exclude= ['info_type'] 
      return Inline 

     if not cls.__inlines: 
      for info_model in [Information.getProxy(info_type) for info_type in InformationType.objects.all()]: 
       cls.__inlines.append(get_inline(info_model)) 
     return cls.__inlines