我有两个模型,Client
和PaymentOptions
。
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中我有这个:
admin.site.disable_action('delete_selected')
class ClientAdmin(admin.ModelAdmin):
#display list
list_display = ('name','contact','address')
#display fields
fields = ('name','contact','address')
pass
admin.site.register(Client, ClientAdmin)
class PaymentOptionAdmin(admin.ModelAdmin):
list_display = ('name', 'action', 'percent', 'fixamount', 'itemcharged', 'client')
fields = ('name', 'action','percent', 'fixamount', 'itemcharged', 'client')
admin.site.register(ClientPaymentOption, ClientPaymentOptionAdmin)
是否有可能每当我在Client
中创建新的django admin
时,它也会自动创建ClientPaymentOption
?
我尝试在admin.py中执行此操作:
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
def save_model(self, request, obj, form, change):
ClientPaymentOption.objects.create(name='MasterCard',action='% Charged', client=obj.id, percent=0.10)
ClientPaymentOption.objects.create(name='Visa', action='% Charged',client=obj.id, percent=0.10)
但我收到了这个错误:
ValueError at /admin/client_app/client/add/
Cannot assign None: "ClientPaymentOption.client" does not allow null values.
当我尝试print
参数时,我注意到客户端仍未保存...
print obj.id
None
有人对我的案子有任何想法或建议吗?
提前感谢...
答案 0 :(得分:1)
在尝试创建新对象之前调用父方法。