Django:如何连续显示嵌套的外键?

时间:2013-08-05 07:10:45

标签: python django django-models django-admin

出于学术目的,我正在创建可以管理公司客户,项目和员工的软件。我想通过在一个单独的模型中引用一个外键,你可以得到两个模型,彼此相邻显示并相关。问题是,它只为每个模型显示一个项目。

例如:您有一个公司正在进行的项目。在admin部分中,它显示项目名称和单个工作人员。但是,它应该显示2名以上的工作人员。

这是文件布局(我尝试为不同的字段客户端创建一个单独的应用程序作为主要客户端并投影次要版本):

clients.admin:

from django.contrib import admin
from django.db import models
from clients.models import Status, Project, Staff_Member, Client

class StatusInline(admin.TabularInline):
    model = Status
    extra = 0



class Staff_MemberInline(admin.TabularInline):
    model = Staff_Member
    extra = 5
    project = models.ForeignKey(Project)

class ProjectInline(admin.TabularInline):
    model = Project

    extra = 1

class ClientAdmin(admin.ModelAdmin):
    fieldsets = [
        (None,  {'fields': ['name']}),


    ]
    inlines = [ProjectInline, StatusInline]

    search_fields = ['name']



admin.site.register(Client, ClientAdmin)
#admin.site.register(Project)
admin.site.register(Staff_Member)

下一个文件

clients.models:

from django.db import models
import datetime
from django.utils import timezone
# Create your models here.
class Client(models.Model):
    name = models.CharField(max_length=200)


    def __unicode__(self):
        return self.name
    #def was_published_recently(self):
    #   return self.pub_date >= timezone.now() - datetime.timedelta(days=1)
    #was_published_recently.admin_order_filed = 'pub_date'
    #was_published_recently.boolean = True
    #was_published_recently.short_description = 'Published recently?'

class Status(models.Model):
    client = models.ForeignKey(Client)
    #staff_member = models.ForeignKey(Staff_Member)
    #project = models.ForeignKey(Project)
    status_text = models.CharField(max_length=200)
    def stat(self):
        return self.status_text
    def __unicode__(self):
        return self.status_text









class Staff_Member(models.Model):
    #status = models.ForeignKey(Status)
    #project = models.ForeignKey(Project)
    #client = models.ForeignKey(Client)
    staff_member_text = models.CharField(max_length=200)


    def __unicode__(self):
        return self.staff_member_text

    def mem(self):
        return self.staff_member_text

class Project(models.Model):
    client = models.ForeignKey(Client)
    project_text = models.CharField(max_length=200)
    staff_member = models.ForeignKey(Staff_Member)

    #status = models.ForeignKey(Status)
    def __unicode__(self):
        full_name = self.client.name +'\'s '+ self.project_text
        return full_name
    def project_name(self):
        return self.project_text
    name = client.name

下一个文件

projects.admin:

from django.contrib import admin
from django.db import models
from clients.models import Project, Status, Client
from clients.admin import StatusInline, Staff_MemberInline

class ClientInline(admin.TabularInline):
    model = Client
    extra = 1

#class ProjectAdmin(admin.ModelAdmin):
#   inlines = [Staff_MemberInline, ClientInline]

#admin.site.unregister(Project)

admin.site.register(Project)#, ProjectAdmin)    

下一个文件

projects.models

from django.db import models
import datetime
from django.utils import timezone
from django.db import models
from clients.models import *
# Create your models here.

我对stackoverflow很新,所以如果我有一种方法可以让这个让眼睛更友好,请告诉我。

谢谢!

1 个答案:

答案 0 :(得分:0)

extra = 1更改为更高的数字。更多信息in the docs