[Answered ]-Stackedinline with field id

1๐Ÿ‘

โœ…

You can create a method which returns the id of the object.

models.py

class POS(models.Model):
    # your fields...

    def get_id(self):
        return self.id

Now, add this method to fields and readonly_fields of PosInLine class.

admin.py

class PosInLine(admin.StackedInline):
    model = POS
    extra = 0
    fields = ('get_id', 'name',)
    readonly_fields = ('get_id',)

Note: The id shown for a new object will not be a correct id. On saving that object, a new correct id will be assigned to the object.

๐Ÿ‘คxyres

1๐Ÿ‘

If your id is also your primary key, you can display that instead:

class PosInLine(admin.StackedInline):
    model = POS
    extra = 0
    fields = ('name',)
    readonly_fields = ('pk',)
๐Ÿ‘คPaddi

Leave a comment