django admin中的方法

时间:2012-03-05 06:10:52

标签: python django django-admin

我有两个模型,ClientPaymentOptions

class Client(models.Model):
    name = models.CharField(max_length=50, null=True, unique=False)
    contact = models.CharField(max_length=50, null=True, unique=False)
    address = models.CharField(max_length=300, null=True, unique=False)

class ClientPaymentOption(models.Model):
    name = models.CharField(max_length=30, null=True, unique=False, choices=CARD_TYPE)
    action = models.CharField(max_length=30, null=True, unique=False, choices=CLIENT_PAYMENT_OPTION)
    percent = models.FloatField(max_length=10, null=True, unique=False)
    fixamount = models.FloatField(max_length=20, null=True, unique=False)
    itemcharged = models.CharField(max_length=10, null=True, unique=False)

在我的admin.py中,我只想创建一个新的Client,它还会创建它ClientPaymentOption

我在admin.py中有这个:

admin.site.disable_action('delete_selected')
class ClientAdmin(admin.ModelAdmin):
    #display list
    list_display = ('name','b_type','banner','logo',
                    'contact','address','account_type',
                    'status','currency','color','user',
                    )
    #display fields
    fields = ('name','b_type','banner','logo','contact',
              'address','account_type','status',
              'currency','color','user',
              )
    pass

admin.site.register(Client, ClientAdmin)

我在views.py,

中完成了这项工作
client = Client.objects.create( .... )
ClientPaymentOption.objects.create( ...., client=client )

但我不知道在django admin中这样做。

有人对我的情况有所了解吗?提前谢谢......

1 个答案:

答案 0 :(得分:0)

您可以覆盖标准django-admin change_view并添加ClientPaymentOption.objects.create( ...., client=client ) 那里。 来自django docs的简短例子:

class MyModelAdmin(admin.ModelAdmin):

    # A template for a very customized change view:
    change_form_template = 'admin/myapp/extras/openstreetmap_change_form.html'

    def get_osm_info(self):
        # ...
        pass

    def change_view(self, request, object_id, extra_context=None):
        extra_context = extra_context or {}
        extra_context['osm_data'] = self.get_osm_info()
        return super(MyModelAdmin, self).change_view(request, object_id,
            extra_context=extra_context)