RMittelman RMittelman - 23 days ago 6
MySQL Question

Problems with TabularInline in Django Admin

I'm porting an Access application to Python/Django. There is a SchoolInfo table and a Campuses table with a foreign key pointing to the SchoolInfo table. In Access, I have a Campuses subform (continuous) embedded into the SchoolInfo form with match fields defined properly pointing to School_ID. So for School 1 I only show campuses for that school.

In Django, I added the SchoolInfo table to Admin and am trying to replicate the Access UI using TabularInline. Here are snippets of model and admin:

class Schoolinfo(models.Model):
school_id = models.AutoField(db_column='School_ID', primary_key=True) # Field name made lowercase.
schoolcode = models.CharField(db_column='SchoolCode', max_length=10) # Field name made lowercase.
schoolname = models.CharField(db_column='SchoolName', max_length=50, blank=True, null=True) # Field name made lowercase.

class Campuses(models.Model):
campus_id = models.AutoField(db_column='Campus_ID', primary_key=True) # Field name made lowercase.
campusno = models.IntegerField(db_column='CampusNo', default=1) # Field name made lowercase.
schoolcode = models.CharField(db_column='SchoolCode', max_length=10) # Field name made lowercase.
school = models.ForeignKey('Schoolinfo', models.DO_NOTHING, db_column='School_ID', unique=True) # Field name made lowercase.# school = models.IntegerField(db_column='School_ID') # Field name made lowercase.
campusname = models.CharField(db_column='CampusName', max_length=15, blank=True, null=True) # Field name made lowercase.

class CampusesInline(admin.TabularInline):
model = Campuses

admin.site.register(Campuses)

class SchoolInfoAdmin(admin.ModelAdmin):
fields = (
('schoolcode', 'schoolname'), ('contactname', 'contacttitle'), ('contactphone', 'contactemail'),
('clockhoursschool', 'schoolsendsawardletters'), ('tin_no', 'duns_no'), ('pell_id', 'ope_id'))

save_on_top = True
inlines = [CampusesInline,]

admin.site.register(Schoolinfo, SchoolInfoAdmin)


When I select a school for edit, it properly shows the school fields, then the TabularInline for campuses, but here is the problem:

There are multiple schools in the database, each having 1 or more campuses. The TabularInline properly shows the campus or campuses for the active school, but it also has several blank records (which I assume are related to the campus records for other schools).

If I add "unique=True" to the foreign key (as shown in the model above), the blank records disappear and all looks good. Unfortunately, I also loos the link at the bottom of the inline to add a new campus.

I'm VERY new to Python/Django, so can't figure out how to fix this. I suppose my choices are some type of filtering in the inline (and take out the unique=True from the model), or leave that in and figure out how to restore the "add" link to the inline.

Are there any suggestions on how to fix this?
Thanks...

Answer

but it also has several blank records (which I assume are related to the campus records for other schools).

Those "blank" records are for adding new campuses to the school. You can use the "extra" attribute on your CampusInline to control the number of blank lines.

If I add "unique=True" to the foreign key (as shown in the model above), the blank records disappear and all looks good. Unfortunately, I also loos the link at the bottom of the inline to add a new campus.

If you add "unique" to the "school" field, that would mean that a school could only appear once in the campus table. Which doesn't (usually) make much sense for a foreign key.

Comments