limit insert rows into model database

21 views
Skip to first unread message

Tony Peña

unread,
Aug 12, 2015, 9:59:00 AM8/12/15
to django...@googlegroups.com
Hi
exist some settings to only insert X number of rows in a model?

django.db. (i guess, restrict 5 rows)   as simple way ?

or

setting directly into MySQL  ?

because, i override the save method to check if:

# models.py
class domain(model.Model):
    ...
    max_accounts = models.IntegerField(default=2)


# admin.py

class UserAdmin(admin.ModelAdmin):
    list_display = ('user', 'domain',  'max_account')
    ordering = ('user',)

    def save_model(self, request, obj, form, change):
        max_accounts = domains.objects.all()
        accounts = users.objects.all()
        if  int(max_accounts.values()[0]['max_accounts']) > accounts.count():
            super(UserAdmin, self).save_model(request, obj, form, change)
        else:
            messages.error(request, "Impossible create more accounts.")

In this code .. the check works... but my problem is:

when account 1 and total is 2 is fine.. save in mysql
when account 2 and total is 2 is fine and save in mysq again
when account is 3 and total is 2 i got the error... not save (because the logic is fine)

and
django admin output the error message BUT show the OK was successfull inserted too
(as a fake insert) ...how can I omit the OK message.?

thanxs in advance

--
Antonio Peña
Secure email with PGP 0x8B021001 available at https://pgp.mit.edu
Fingerprint: 74E6 2974 B090 366D CE71  7BB2 6476 FA09 8B02 1001
Reply all
Reply to author
Forward
0 new messages