0

Django の ManyToMany 関係に問題があるようです。これについて適切な管理画面が表示されていないようです。住所フィールドにstackedinlineを表示したかったのですが、ドロップボックスしか得られません。以下は私のコードです。

モデル.py

class AddressType(models.Model):
    TypeCode = models.CharField('Address Type', max_length=50, blank=False, null = False)
    Description = models.TextField('Description')
    def __unicode__(self):
        return self.TypeCode

class Address(models.Model):
    AddressType = models.ForeignKey(AddressType)
    Address = models.TextField('Address', blank=False, null=False)

    def __unicode__(self):
        return self.AddressType.TypeCode


class BusinessPartner(models.Model):
    FullName = models.CharField('Full Name', max_length=50, blank=False, null=False)
    PartnerType = models.ForeignKey(PartnerType)
    Company = models.CharField('Company', max_length=100, blank=True, null=True)
    Website = models.URLField('Website', blank=True, null=True)
    Address = models.ManyToManyField(Address,through='AddressPartner_Assn')

    def __unicode__(self):
        if self.Company:
            return self.FullName + '-' + self.Company
        else:
            return self.FullName

class AddressPartner_Assn(models.Model):
    Address = models.ForeignKey(Address)
    BusinessPartner = models.ForeignKey(BusinessPartner)

Admins.py
=========
class AddressTypeAdmin(admin.ModelAdmin):
    list_display = ('TypeCode','Description')
    search_fields = ['TypeCode','Description']

class AddressAdmin(admin.ModelAdmin):
    list_display = ['Address']
    search_fields = ['Address']
    list_filter = ['AddressType']

class AddressInlineAdmin(admin.StackedInline):
    model = BusinessPartner.Address.through
    extra = 1

class BusinessPartnerAdmin(admin.ModelAdmin):
    list_display = ('__unicode__','FullName','PartnerType','Company')
    inlines = [AddressInlineAdmin,]
    search_fields = ['FullName','Company']
    list_filter = ('PartnerType',)
    raw_id_fields = ('PartnerType',)
4

1 に答える 1

1

through 引数を使用して中間テーブルを明示的に言及しているため、 AddressInlineAdmin を次のように置き換える必要があります。

class AddressInlineAdmin(admin.StackedInline):
    model = AddressPartner_Assn
    extra = 1

追加情報については、 https://docs.djangoproject.com/en/1.4/ref/contrib/admin/#working-with-many-to-many-intermediary-modelsを参照してください。

于 2013-01-14T08:22:05.150 に答える