#15602: Using get_readonly_fields and StackedInline/TabularInline admin objects
doesn't allow creating new objects, immutible existing objects
---------------------------------+------------------------------------
Reporter: bradwhittington | Owner: nobody
Type: Bug | Status: new
Component: contrib.admin | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
---------------------------------+------------------------------------
Comment (by John):
I'm using Django 2.0.7 and have the same issue. However, I've a dumb
solution to this (untested).
I'm building an account-transaction admin page. As one would expected,
the AccountAdmin will have TransactionStackedInlines. Undoubtably, we use
the inline form to add new transaction with most fields editable. And for
existing transaction, we want them as read-only.
Since, existing transactions look better in tabular inline (not all fields
displayed), while new transaction is better in stacked form, I created two
inlines. Here is my codes:
{{{
@admin.register(models.Account)
class AccountAdmin(admin.ModelAdmin):
inlines = [
inlines.TransactionTabularInline,
inlines.TransactionStackedInline,
]
}}}
And for the two inlines, I want that the top, tabular inline has no "add
new" action, while the bottom stacked inline shows nothing but a new
transaction:
{{{
class TransactionTabularInline(admin.TabularInline):
model = models.Transaction
extra = 1
max_num = 0
fields = (contains only a handful of fields)
can_delete = False
show_change_link = True
readonly_fields = [
the list of 'fields'
]
class TransactionStackedInline(admin.StackedInline):
model = models.Transaction
extra = 0
def get_queryset(self, request):
queryset = super().get_queryset(request)
return queryset.none()
}}}
There will be an arrow on each row in tabular inline, which points to
TransactionAdmin, in which `get_readonly_fields` will work as every one
expected.
I think you can use two (stacked) inlines, one for existing records with
more `readonly_fields`, and one without readonly but return an empty
query_set.
--
Ticket URL: <https://code.djangoproject.com/ticket/15602#comment:19>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/073.d8e04ae40472e1aef4f37f4a8c103bda%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.