0

In Django admin.ModelAdmin has function with name def has_add_permission(), there is you can manage for limit in your models, for complete docs you can checkout in this: https://docs.djangoproject.com/en/1.9/ref/contrib/admin/#django.contrib.admin.ModelAdmin.has_add_permission

Some time ago, we was try it for setup my appearance, that is to manage some view in my project, such as widgets, setting of header image, or something else.

for example like this:

Django Limit single models

You can see it, some models haven’t add button, but just change button right? And if we click button of change, we only have once object. Thereis usefull if you manage it for your appearance, or other models if need for limit.

Django Limit single models

Ok, thereis sample of my last project in appereance/models.py

class Data_Toko(models.Model):
    nama_toko = models.CharField(max_length=200, .... )
    nama_kontak = models.CharField(max_length=200, .... )
    nomor_hp = models.CharField(max_length=200, .... )
    email_kontak = models.EmailField(max_length=70, .... )
    pin_bb = models.CharField(max_length=200, .... )
    jam_kerja = models.CharField(max_length=200, .... )
    ym = models.CharField(max_length=200, ... )
    kode_pos = models.CharField(max_length=200, .... )
    alamat_toko = models.TextField(blank=True, .... )

And thereis in my appereance/admin.py

class AdminData_Toko(admin.ModelAdmin):
    def has_add_permission(self, request):
        count = models.Data_Toko.objects.all().count()
        if count == 0:
            return True
        return False
    inlines = [ Data_BankInline, PengirimanInline ]
    #http://stackoverflow.com/a/1744292
    formfield_overrides = {
        base_model.TextField: {'widget': Textarea(attrs={'rows': 5, 'cols': 82,})},
    }

So, the logic is: if count of models is nothing, for user has add permission can add it, but if if objects was exists, user can’t add it again.

Helpfully can help..

Refference:

python django

Your Answer

blog comments powered by Disqus