开发者

How do I Display Intermediary Fields for M2M Models in admin interface?

I have following model.py file in which Charges model have M2M field farms with an intermediate model Membership.

class Farm(mode开发者_运维问答ls.Model):
    farmNo = models.CharField(max_length=100)
    size = models.CharField(max_length=50)
    description = models.TextField()

class Charges(models.Model):
    chargeDescription = models.TextField(verbose_name="Description")
    chargeType = models.CharField(max_length=10,choices=CHARGE_TYPE)
    farms = models.ManyToManyField(Farm,through='Membership')

    class Meta:
        verbose_name_plural = "Charges"

class Membership(models.Model):
    farm = models.ForeignKey(Farm)
    charges = models.ForeignKey(Charges)
    paymentDate = models.DateField()
    paymentStatus = models.BooleanField(default=False)

My problem is that when i create a new object using django admin app for Charges model "farms" M2mM field doesnot shows up and that way i cannot assign charges to farms.I am wondering why the widget is not showing up and how can I again show that widget for user to select farms while create a charge.

I tried overriding def formfield_for_manytomany(self, db_field, request=None, **kwargs): method which is causing widget not to show on screen but still its not appearing on screen.

Please help Thanks


It won't because it can't add a Farm without collecting the additional data on Membership. You have to use inlines, but even then, you have to inline Membership, not Farm.

class MembershipInlineAdmin(admin.TabularInline):
    class Meta:
        model = Membership
        extra = 1

class ChargesAdmin(admin.ModelAdmin):
    inlines = [
        MembershipInlineAdmin,
    ]

admin.site.register(Charges, ChargesAdmin)

You can then select an existing Farm or create a new Farm in the normal way on each Membership you add.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜