Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django admin site reverse ForeignKey inline

I have these models:

(pseudocode)

Course:  
  ForeignKey(Outline, null=True, blank=True)  
  ForeignKey(OutlineFile, null=True, blank=True)

Outline:    
  //data

OutlineFile:    
  //different data

The situation is that any course can have an Outline associated with it, and/or an OutlineFile, or neither. An Outline can be associated with multiple courses, similarly an OutlineFile can be associated with multiple courses. However, a course will only ever have at most one of each.

What I want is to have the Course change admin page show all the Course fields, and a drop down for each of Outline and OutlineFile. If one is then selected, I want the fields for that Outline to be displayed and modifiable, just like an inline field.

Should I be restructuring my models somehow, or are they structured adequately already? Is there anyway to do what I want within the confines of the current inlines system?

Lastly, if it's not possible, where do I start in doing it in a custom fashion?

like image 335
Jason Kotenko Avatar asked Dec 22 '09 22:12

Jason Kotenko


1 Answers

You are doing it the other way around:

class Course(models.Model):  
  # Foreign key is defined only in related fields

class Outline(models.Model):    
  course = models.ForeignKey(Course,
    related_name='outlines', # Or whatever you choose
    null=True, # These two mean your FK relation is basically optional
    blank=True
    )

class OutlineFile(models.Model):    
  # Same structure as above

When you create the std forms, this model structure will create a dropdown like you specify by default.

like image 180
e.barojas Avatar answered Nov 13 '22 05:11

e.barojas