M
M
Mooris2017-05-17 00:49:22
Django
Mooris, 2017-05-17 00:49:22

Django, InlineModelAdmin.get_extra error, what's the problem?

Hello, the problem is this, it was necessary to return the number of additional built-in forms, I took this code from the documentation

InlineModelAdmin.get_extra
class BinaryTreeAdmin(admin.TabularInline):
    model = BinaryTree

    def get_extra(self, request, obj=None, **kwargs):
        extra = 2
        if obj:
            return extra - obj.binarytree_set.count()
        return extra

returns everything fine, but when adding and saving it throws an error
django.core.exceptions.ValidationError: ["Value '' must be an integer."]
my model is like this
class Jurnal(models.Model):
    pos = models.AutoField(unique=True, primary_key=True)
    client = models.ForeignKey(Client, on_delete=models.CASCADE, db_column='client_id',)
    data = models.DateField(default=date.today, blank=True, null=True)
    time = models.TimeField(default=datetime.datetime.now, blank=True)

admin.py
class JurnalInline(admin.TabularInline):
    model = Jurnal
    def get_extra(self, request, obj=None, **kwargs):
        extra = 5
        if obj:
            return extra - obj.jurnal_set.count()
        return extra

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question