0

In my model I have a getter and setter for a property:

class SomeModel(ext_models.TimeStampedModel):
    ...

    @property
    def test_field(self):
        return {'key1': 'val1', 'key2': 'val2'}

    @test_field.setter
    def test_field(self, value):
        print(value)

I'd like to use the test_field in the admin as a dropdown. I have tried to add it in the fieldsets, but I receive the error

Unknown field(s) test_field specified 

I have also added it to the readonly_fields and then the field is at least displayed, however just a string and not editable of course.

Is there a nice way to get a simple dropdown widget displaying the dict?

wasp256
  • 5,943
  • 12
  • 72
  • 119
  • 1
    What do you mean, "as a selector"? A selector for what, where? – Daniel Roseman Sep 14 '18 at 10:07
  • I want a selector in the admin with the values from the property `test_field` – wasp256 Sep 14 '18 at 10:30
  • @DanielRoseman sorry I used the wrong term, I meant dropdown – wasp256 Sep 14 '18 at 11:10
  • 2
    But the question is what should happen when selecting an option from the dropdown? What are you trying to achieve? And is this in the admin 'change' view or the admin 'list' view? – dirkgroten Sep 14 '18 at 11:11
  • It is the admin change view; when a new value in the dropdown is selected (and saved) the setter of the propterty should be triggered to perform a custom handling – wasp256 Sep 14 '18 at 11:22
  • You can only edit model fields in the admin change view. Since `test_field` isn't a model field, that won't work. You need to override the `ModelForm` for this view and add this field to it, handling validation and saving of it in your form. See for example [here](https://stackoverflow.com/questions/17948018/django-admin-add-custom-form-fields-that-are-not-part-of-the-model) – dirkgroten Sep 14 '18 at 15:48

0 Answers0