从父管理员访问InlineModelAdmin的表单数据

时间:2015-10-15 20:18:27

标签: python django

我有一个CateringOrder模型的模型管理员,它有一个多对多表中的餐馆列表。这些可以在我已包含的InlineModelAdmin中添加和修改。

我想访问内联表单中的表单字段。我注意到它们可以在form.data中访问,但我觉得必须有一个更简单的方法。

这是我的代码:

class CateringOrderRestaurantForm(forms.ModelForm):
    notify = forms.BooleanField(label="Notify?", required=False)

    def save(self, _request, catering_order_restaurant, form, change):
        import pdb;pdb.set_trace()
        catering_order_restaurant.save()


class CateringOrderRestaurantInline(admin.TabularInline):
    model = CateringOrderRestaurant
    form = CateringOrderRestaurantForm

    notify = forms.BooleanField(label="Notify?", required=False)
    fields = ["restaurant", "priority"]
    list_display = ("restaurant", "priority", "notify")
    raw_id_fields = ["restaurant"]
    extra = 1

class CateringOrderAdmin(admin.ModelAdmin):
    fields = [
        "status",
        "super_hub",
        "delivery_location",
        "delivery_date",
        "delivery_preference",
        "non_vegetarians",
        "vegetarians",
        "vegans",
        "headcount",
        "rough_budget_per_head",
        "dietary_notes",
        "additional_requests",
        "delivery_notes",
    ]

    list_display = ('id', 'delivery_location', 'super_hub', 'status', 'delivery_date', 'headcount', 'budget')
    list_filter = ('status', 'delivery_date', )
    readonly_fields = ('status', 'headcount',)
    search_fields = ('delivery_location__company__name', 'delivery_preference',)
    raw_id_fields = ["delivery_location"]
    actions = ['cancel', 'finalize_orders']

    inlines = (
        CateringOrderRestaurantInline,
        CateringOrderItemInline
    )

    def save_model(self, _request, catering_order, form, change):
        import pdb;pdb.set_trace()
        catering_order.save()

        if not change:
            email_inquiry(catering_order)

我需要在CateringOrderAdmin上覆盖的save_model方法中访问notify字段的值。

1 个答案:

答案 0 :(得分:0)

所以我想通了,如果你覆盖了save_related方法,那么将传入所需的数据。只是使用了错误的方法......