python - Django Admin 中的 TabularInline 问题

标签 python mysql django

我正在将 Access 应用程序移植到 Python/Django。有一个 SchoolInfo 表和一个 Campuses 表,其中一个外键指向 SchoolInfo 表。在 Access 中,我有一个嵌入到 SchoolInfo 表单中的 Campuses 子表单(连续),其中正确定义了指向 School_ID 的匹配字段。因此,对于学校 1,我仅显示该学校的校园。

在 Django 中,我将 SchoolInfo 表添加到 Admin 中,并尝试使用 TabularInline 复制 Access UI。以下是模型和管理的片段:

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)

当我选择一所学校进行编辑时,它会正确显示学校字段,然后显示校园的 TabularInline,但问题是:

数据库中有多所学校,每所学校都有 1 个或多个校区。 TabularInline 正确显示了当前学校的一个或多个校园,但它也有几个空白记录(我认为这些记录与其他学校的校园记录相关)。

如果我将“unique=True”添加到外键(如上面的模型所示),空白记录就会消失,一切看起来都很好。不幸的是,我也丢失了内联底部添加新校区的链接。

我对 Python/Django 非常陌生,所以不知道如何解决这个问题。我想我的选择是在内联中进行某种类型的过滤(并从模型中取出 unique=True ),或者将其保留并找出如何恢复到内联的“添加”链接。

对于如何解决这个问题有什么建议吗? 谢谢...

最佳答案

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

那些“空白”记录用于为学校添加新校区。您可以使用 CampusInline 上的“额外”属性来控制空行数。

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.

如果将“unique”添加到“学校”字段,则意味着学校只能在校园表中出现一次。这对于外键来说(通常)没有多大意义。

关于python - Django Admin 中的 TabularInline 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40553574/

相关文章:

python - 如何计算python3中defaultdict的唯一记录

mysql - 从两个表连接中获取前两条唯一记录 - Mysql

django: sqlite3.OperationalError: 没有这样的表

python - django-tables 2 M2M 字段未显示

python - 在 python 单元测试中模拟类属性的更好方法

python - 使用 Appengine 的导航台从实体的 ID 获取 key

python - 如何使用python获得具有所需精度的 float

mysql - 亚马逊 RDS(Mysql2::错误 110)

php - 电子商务网站中的优惠券代码验证

python - Django - 将用户设置为不活动 5 秒