0

Expanding from this question: Can "list_display" in a Django ModelAdmin display attributes of ForeignKey fields?, could it be possible to do something like this:

class MyModelInline(admin.StackedInline):
    model  = MyModel
    extra  = 1
    fields = ('my_field',)

    def my_field(self, obj):
        return obj.one_to_one_link.my_field

If something like this were possible, it would solve most of my current Django problems, but the code above does not work: Django (rightly) complains that my_field is not present in the form.

Community
  • 1
  • 1
eje211
  • 2,385
  • 3
  • 28
  • 44

1 Answers1

1

You can do that, but you must also add my_field to your MyModelInline class's readonly_fields attribute.

fields = ('my_field',)
readonly_fields = ('my_field',)

From the docs:

The fields option, unlike list_display, may only contain names of fields on the model or the form specified by form. It may contain callables only if they are listed in readonly_fields.

If you need the field to be editable, you should be able to do that with a custom form but it takes more work to process it.

Peter DeGlopper
  • 36,326
  • 7
  • 90
  • 83
  • Thank you! I must have missed that part of the docs. I was about to start a whole thing with proxy models and custom fields. Thanks for your help! – eje211 Oct 29 '13 at 00:00