gadss
gadss

Reputation: 22499

how to put methods in django admin?

I have two models, Client and 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)

in my admin.py I have this :

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)

is it possible that whenever I create a new Client in django admin it will also automatically create it's ClientPaymentOption?

I try to do this in my 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)

but i got this error:

ValueError at /admin/client_app/client/add/
Cannot assign None: "ClientPaymentOption.client" does not allow null values.

when I try to print the parameters, i have noticed that the Client still not saved...

print obj.id
None

do anyone have an idea or suggestion about my case?

thanks in advance ...

Upvotes: 0

Views: 177

Answers (1)

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 799044

Call the parent's method before attempting to create the new objects.

Upvotes: 1

Related Questions