Django Python - 如何将外键添加到管理页面中显示的 table

Django Python - How to add foreign key to table displayed in admin page

所以我在 MySQL 数据库中得到了 4 tables。 在我用 Django 创建的应用程序下,数据库由 Django 管理。 这里是文件models.py中的4个class中的2个对应数据库tables:

from __future__ import unicode_literals

from django.db import models

class Pdb(models.Model):
    id_pdb_chain = models.CharField(db_column='id_PDB_chain', primary_key=True, max_length=5)  # Field name made lowercase.
    id_pdb = models.CharField(db_column='id_PDB', max_length=4)  # Field name made lowercase.
    chaine = models.CharField(max_length=10)
    header = models.CharField(max_length=255)
    sequence_proteine = models.TextField(db_column='sequence_Proteine')  # Field name made lowercase.
    start_seq = models.IntegerField()
    taille_proteine = models.IntegerField(db_column='taille_Proteine')  # Field name made lowercase.
    resolution_pdb = models.FloatField(db_column='resolution_PDB')  # Field name made lowercase.
    meth_res = models.ForeignKey('MethodesRes', models.DO_NOTHING, db_column='meth_Res')  # Field name made lowercase.

    def __unicode__(self):
        return self.id_pdb

    class Meta:
        managed = False
        db_table = 'PDB'

class StructSec(models.Model):
    id_struct_sec = models.AutoField(primary_key=True)
    start_pred = models.IntegerField()
    structure_predite = models.TextField(db_column='structure_Predite')  # Field name made lowercase.
    nombre_ppii = models.IntegerField(db_column='nombre_PPII')  # Field name made lowercase.
    pourcentage_ppii = models.FloatField(db_column='pourcentage_PPII')  # Field name made lowercase.
    angle_phi = models.TextField()
    angle_psi = models.TextField()
    id_pdb_chain = models.ForeignKey(Pdb, models.DO_NOTHING, db_column='id_PDB_chain')  # Field name made lowercase.
    nom_analyse = models.ForeignKey(MethodesAnalyse, models.DO_NOTHING, db_column='nom_Analyse')  # Field name made lowercase.

    def __unicode__(self):
        return self.structure_predite

    class Meta:
        managed = False
        db_table = 'struct_sec'

如您所见,我已经找到了一种方法,可以在管理页面中为每个 table 显示一个字段,每个 table:

中都有这个部分
def __unicode__(self):
    return self.structure_predite

但是当我想用 "StructSec" class 中的主键替换 "structure_predite" 时,像这样:

def __unicode__(self):
    return self.id_struct_sec

Django return 这个错误:

TypeError: coercing to Unicode: need string or buffer, long found

我还必须提到,我在 "admin.py" 文件中做了一些修改,这里是:

from django.contrib import admin

from .models import Pdb, MethodesAnalyse, MethodesRes, StructSec


class PdbInline(admin.TabularInline):
    model = Pdb

class PdbAdmin(admin.ModelAdmin):
    list_display = ('id_pdb','header','chaine','taille_proteine','meth_res')
    list_filter = ['chaine','meth_res']
    search_fields = ['id_pdb_chain','header']


class MethodesAnalyseInline(admin.TabularInline):
    model = MethodesAnalyse

class MethodesAnalyseAdmin(admin.ModelAdmin):
    list_display = ('nom_analyse')


class MethodesResInline(admin.TabularInline):
    model = MethodesRes

class MethodesResAdmin(admin.ModelAdmin):
    list_display = ('meth_res')


class StructSecInline(admin.TabularInline):
    model = MethodesRes

class StructSecAdmin(admin.ModelAdmin):
    list_display = ('id_struct_sec','nombre_PPII','pourcentage_PPII','id_PDB','nom_Analyse')
    search_fields = ['nombre_ppii','pourcentage_ppii']


admin.site.register(Pdb,PdbAdmin)
admin.site.register(MethodesAnalyse)
admin.site.register(MethodesRes)
admin.site.register(StructSec)

知道了这一点,我的问题就简单了: 如何在相应的管理 table 中正确显示来自 class StructSec 的自动递增 ID(主键)?

我认为错误是由于 None 值引起的。 Python 无法转换 None.

将您的 unicode 定义替换为 'str' 为:

 def __str__(self):
      return self.id_struct_sec

Sijan 的回答是正确的,我只是补充一点,要使链接有效,您可能还必须这样做:

 def __str__(self):
      return str(self.id_struct_sec)

如果 return 中没有 "str()",链接将不起作用,并且在单击时会出现 return 错误。