0

本周早些时候,我问了这个关于在你的子类中在你的主模型中使用外键的问题:Django Form With Foreign Key

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

抱歉,这听起来令人困惑,这是我的模型:

子类 1 预观察

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 )                                  
4

1 回答 1

1

您可以使用

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

  admin.site.register(Record, RecordAdmin)
于 2013-03-04T09:46:48.780 回答