Olivier Van Bulck
Olivier Van Bulck

Reputation: 786

Django - Switch between boolean and integer in TabularInline

I have some models in Django like this:

class Object(models.Model):
    ...

class ObjectFeatures(models.Model):
    class Meta:
        unique_together = (('object', 'feature'),)
    count = models.PositiveSmallIntegerField()
    object = models.ForeignKey(...)
    feature = models.ForeignKey(...)

class Feature(models.Model):
    is_number = models.BooleanField()
    ...

I have an object, and in this object is an inline form in the admin panel with ObjectFeature. There you can select the feature you want to add from features, and the count of how many of that feature are available.

The is_number defines if the feature expects a number or, if false, a boolean (0 or 1 in count).

Is there anyway to display a checkbox in the TabularInline when it expects a boolean rather than an integer, though it's an IntegerField?

Another possibility is to define a boolean and an integer field in object_features. Is there a possibility I can show just one of those, based on the value in is_number?

In admin.py:

class ObjectFeatureInline(admin.TabularInline):
    model = ObjectFeature
    can_delete = True
    verbose_name_plural = 'Object features'

class ObjectAdmin(admin.ModelAdmin):
    inlines = (ObjectFeatureInline,)
    ...

Upvotes: 0

Views: 462

Answers (1)

Ohad the Lad
Ohad the Lad

Reputation: 1929

IN your inline

    def new_field(self, obj):
        if type(obj.is_number) is bool:
            do stuff - as bool
        else:
            do other stuff , maybe check if int and so on...
    new_field.allow_tags = True
    new_field.short_description = 'is_number verbose'

add new_field to readonly_fields in inline

Upvotes: 0

Related Questions