0

如何让 Django 在后端将 AnotherAim.aimid.code 的外键显示到 AnotherAimAdmin 的一个表中?

这是一个经典的管理“视图”,在后端由两个不同的表格分隔:

class AimAdmin(admin.ModelAdmin):
    list_display = ('code')

# AnotherAim and AIM are connected via a foreight key

class AnotherAimAdmin(admin.ModelAdmin):
    list_display = ('name','sur_name', 'email')
    search_fields = ['name','sur_name']


admin.site.register(Aim,AimAdmin)
admin.site.register(AnotherAim, AnotherAimAdmin) 

我的模型如下所示:

class AnotherAim(models.Model):

    email=models.EmailField(null=True)
    name= models.CharField(max_length=100)
    sur_name= models.CharField(max_length=100)
    code = models.OneToOneField(Aim,null=True)



class Aim(models.Model):

     code = models.CharField(max_length=5, null=False)
     ....
     ....
4

1 回答 1

1

我会这样构建我的模型:

class Aim(models.Model):
     code = models.CharField(max_length=5)

     def __unicode__(self):
         return self.code

class AnotherAim(models.Model):
    email = models.EmailField(blank=True)
    name = models.CharField(max_length=100)
    sur_name = models.CharField(max_length=100)
    aim = models.OneToOneField(Aim, null=True)

    def __unicode__(self):
        return self.name  # or another field
于 2013-03-14T21:38:18.707 回答