Django - 在字段集中使用相同的模型两次

时间:2015-03-03 20:39:22

标签: django django-models

我想在“管理”面板的字段集中使用Party模型两次(但值不同),但无法弄清楚如何操作。有办法吗?任何帮助表示赞赏!

from django.contrib import admin
from mvp.models import Contract, Party
# Register your models here.

class ContractAdmin(admin.ModelAdmin):

    fieldsets = [
        (None,               {'fields': ('contract_number', 'contract_type', 'contract_startdate', 'contract_enddate','contract_renewaldate', 'contract_value', 'contract_currency', 'party')}),
    ]

    search_fields = ['contract_number', 'party__party_name']
    list_display = ('contract_number', 'contract_type', 'contract_startdate', 'contract_enddate', 'contract_renewaldate')


admin.site.register(Contract, ContractAdmin)
admin.site.register(Party)

这是models.py

class Party(models.Model):
    party_name = models.CharField('Name', max_length=60)
    party_street = models.CharField('Street', max_length=60)
    party_city = models.CharField('City', max_length=60)
    party_zip = models.CharField('Zip/Postal Code', max_length=60)
    party_country = models.CharField('Country', max_length=60)
    party_email = models.CharField('Email', max_length=60)
    party_fax = models.CharField('Fax', max_length=60)
    party_phone = models.CharField('Phone', max_length=60)
    def __unicode__(self):
        return self.party_name
    pass

class Contract(models.Model):
    CONTRACT_TYPES = (
        ('PNS', 'Purchase and Sale'),
        ('CDA', 'Confidential Disclosure Agreement'),
        ('DIS', 'Distribution'),
        ('LIC', 'Licensing Agreement'),
        ('SOW', 'Statement of Work'),
        ('IMP', 'Implementation Agreement'),
        )
    CURRENCY_TYPES = (
        ('CAD', 'CAD'),
        ('EUR', 'EUR'),
        ('USD', 'USD'),
        )
    party = models.ForeignKey(Party)
    contract_number = models.CharField('Contract Number', db_index=True,     max_length=60, primary_key=True)
    contract_type = models.CharField('Contract Type', db_index=True, max_length=3, choices=CONTRACT_TYPES)
    contract_startdate = models.DateField('Start Date')
    contract_enddate = models.DateField('End Date')
    contract_renewaldate = models.DateField('Renewal Date')
    contract_value = models.IntegerField('Value')
    contract_currency= models.CharField('Currency', db_index=True, max_length=3, choices=CURRENCY_TYPES)
    def __unicode__(self):
        return self.contract_type

2 个答案:

答案 0 :(得分:0)

您目前与ForeignKeyContract之间存在Party关系,这意味着您每Party只能选择一个Contract。这被称为“一对多”关系'。你应该做的是将其改为多对多'关系。

更改行:

party = models.ForeignKey(Party)

party = models.ManyToManyField(Party)

然后您可以在ModelAdmin中使用filter_vertical或filter_horizo​​ntal添加/删除派对:

class ContractAdmin(admin.ModelAdmin):
    ....
    filter_horizontal = ('party',)

更新:如果您只需要两方与合同关联,您只需添加第二个字段

party2 = models.ForeignKey(Party, related_name='contract2')

但是在这种情况下,您无法通过简单地写party.contracts来获得Django ORM的好处,例如获得与派对相关的所有合同。如果你永远不需要反向遍历;您可以使用related_name='+'完全禁用它。

答案 1 :(得分:-1)

为派对创建一个固定模型

from django.contrib import admin

    class PartyAdmin(admin.ModelAdmin):
         list_display = ('party_name',)

    class PartyInline(admin.TabularInline):
        model = Party

    class ContracAdmin(admin.ModelAdmin):
    fieldsets = [
            (None,               {'fields': ('contract_number', 'contract_type', 'contract_startdate', 'contract_enddate','contract_renewaldate', 'contract_value', 'contract_currency')}),
        ]

        search_fields = ['contract_number', 'party__party_name']
        list_display = ('contract_number', 'contract_type', 'contract_startdate', 'contract_enddate', 'contract_renewaldate')
        inlines = [
            PartyInline,
        ]


admin.site.register(Contract, ContractAdmin)
admin.site.register(Party, PartyAdmin)