0

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

例如:您的公司正在开展一个项目。在管理部分,它显示项目名称和单个员工。但是,它应该显示 2 个以上的工作人员。

这是文件布局(我尝试为不同领域的客户创建一个单独的应用程序,客户是主要的,项目是次要的):

客户.管理员:

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)

下一个文件

客户模型:

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

下一个文件

项目管理员:

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)    

下一个文件

项目.模型

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还是很陌生,所以如果我想念一种方法来让这对眼睛更友好,请告诉我。

谢谢!

4

1 回答 1

0

更改extra = 1为更高的数字。文档中的更多信息。

于 2013-08-05T20:09:20.647 回答