I have two models named 'Message' and 'Ticket'. Message has a Foreignkey to Ticket. I showed Messages of a Ticket in django admin, using StackedInline. But the problem is I want that the already created messages be read-only, while being able to create new message, too.
I've also check bunch of questions; like this or this. But none of the was helpful! Or at least, I could not get the clue!
This is my code:
models.py:
class Ticket(models.Model):
title = models.CharField(max_length=128)
#...
class Message(models.Model):
text = models.TextField()
ticket = models.ForeignKey(Ticket, on_delete=models.CASCADE)
attachment = models.FileField(upload_to=some_url_pattern)
sender = models.CharField(max_length=2, editable=False)
admin.py:
class MessageInline(admin.StackedInline):
model = Message
extra = 1
def get_readonly_fields(self, request, obj=None):
if obj:
return ['text', 'attachment']
else:
return []
@admin.register(Ticket)
class ResponderAdmin(admin.ModelAdmin):
fields = ['title']
inlines = [MessageInline]
As can be seen, I tried to achieve the goal by overriding get_readonly_fields
but this is what happend:
the screenshot of admin page
As can be seen in the picture, every message inlines has been made read-only and I can not add a new message...
Can anyone help me with this issue?