简体   繁体   English

Django admin-外键显示其值(非内联)

[英]Django admin - Foreign key to display its values (not inlines)

earlier this week, I asked this question about having foreign keys in your main model from your subclasses: Django Form With Foreign Key 本周早些时候,我问了一个有关子类中主键中是否包含外键的问题: Django Form With Foreign Key

I used the answer given to make a model and sub-models (code at the end). 我使用给出的答案来制作模型和子模型(最后是代码)。 My question is, I know about admin inlines for foreign keys, but I can't use that since the main model has the foreign key to the subclasses, not the other way around. 我的问题是,我知道外键的管理内联,但是我不能使用它,因为主模型具有子类的外键,而不是相反。 I want the foreign keys in my main class to be displayed in the admin. 我希望主类中的外键显示在管理员中。

Sorry it that sounds confusing, here's my model: 抱歉,这听起来令人困惑,这是我的模型:

Subclass 1 PreObservation 子类别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 )        

Subclass 2 FieldObservation 子类别2现场观察

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

Main class Record 主班记录

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 )                                  

You can use 您可以使用

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

  admin.site.register(Record, RecordAdmin)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM