Django admin - 显示其值的外键(不是内联)

时间:2013-03-04 04:01:28

标签: python django django-models django-admin

本周早些时候,我问过这个问题,即你的子类中的主模型中有外键:Django Form With Foreign Key

我使用给出的答案来制作模型和子模型(最后的代码)。我的问题是,我知道外键的管理内联,但我无法使用它,因为主模型具有子类的外键,而不是相反。我希望我的主类中的外键显示在管理员中。

对不起,这听起来令人困惑,这是我的模特:

Subclass 1 PreObservation

class PreObservation( models.Model ):                                
    pre_observation = models.CharField(                              
                        max_length=255,                          
                        choices=OBS_STANDARD_TYPES,              
                        verbose_name="Pre-Observation Standard"  
                    )                                            
    obs__meter_reading = models.FloatField( blank=True, null=True )  
    obs_if_other = models.FloatField( blank=True, null=True )        

子类2 FieldObservation

class FieldObservation( models.Model ):                                                          
      site_id = models.CharField( max_length=255, choices=STATION_CHOICES )                        
      site_name = models.CharField( max_length=255 )                                               
      stage_reading = models.FloatField( )                                                         
      specific_conductance = models.FloatField( )                                                  
      water_temp = models.FloatField( )                                                            

主类记录

class Record( models.Model ):                                                                    
      observers = models.CharField( max_length=255, verbose_name="Name of Observer(s)")            

      pre_observation_standard_1 = models.ForeignKey(                                              
                                 PreObservation,                                              
                                 related_name="pre_observation_1"                             
                             )                                                                
      pre_observation_standard_2 = models.ForeignKey(                                              
                                 PreObservation,                                              
                                 related_name="pre_observation_2",                            
                                 blank=True, null=True                                        
                             )                                                                

      field_observation_1 = models.ForeignKey(                                                     
                         FieldObservation,                                                    
                         related_name="field_observation_1"                                   
                       )                                                                      
      field_observation_2 = models.ForeignKey(                                                     
                         FieldObservation,                                                    
                         related_name="field_observation_2",                                  
                         blank=True, null=True                                                
                       )                                                                      

      cloud_coverage = models.CharField( max_length=255, choices=CLOUD_COVERAGE )                  
      rain_past_three_days = models.BooleanField( verbose_name="Rain in Past 3 Days" )             
      snow = models.BooleanField( )                                                                
      snow_melt = models.FloatField( )                                                             
      temperature = models.CharField( max_length=255, choices=TEMPERATURE )                        
      wind = models.CharField( max_length=255, choices=WIND )                                      
      field_notes = models.TextField( )                                                            
      teachers_comments = models.TextField( )                                                      

      user = models.ForeignKey( User )                                                             
      group_name = models.CharField( max_length=255, blank=True )                                  

1 个答案:

答案 0 :(得分:1)

您可以使用

  class RecordAdmin(admin.ModelAdmin):
  list_display = ('pre_observation__pre_observation_standard_1', 
                'pre_observation__pre_observation_standard_2', )

  admin.site.register(Record, RecordAdmin)