0
votes

earlier this week, I asked this question about having foreign keys in your main model from your subclasses: 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

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

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 )                                  
1
what's wrong with the way they're displayed at the moment? - Aidan Ewen
ForeignKey right now is just a list of queries. When I view a query of my Record model in admin, I don't want the ForeignKeys to be displayed as a list, instead, I want to see all the values within each foreign key, sort of like how admin inlines work. For example, when I see pre_observation_standard_1 in my Record class, I don't want a drop down of other PreObservation queries, I actually want to see the fields and the values within the PreObservation class - dajee
It's impossible to connect because Preobservation has no foreignkey for Record. But I came up with an idea. Do you like to create link in the list of record. So every time you click any link of of preobservation you will be redirected to preobservation page then you'll see the equivalent value based on their id? - catherine
Yes, I guess I will have to do something like that, do you know where I can find the information to do something like this? Thanks for the help Catherine :) - dajee

1 Answers

1
votes

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)