Geo
Geo

Reputation: 96767

How do I access a model field's value in this case?

I need to change the widget used in the admin, based on the value of the db_field. Here's where I'm trying to step in:

def formfield_for_dbfield(self,db_field,**kwargs):
    field = super(MyAdmin, self).formfield_for_dbfield(db_field, **kwargs)
    if db_field.name == "my_custom_name":
      # how can I check here the value of the object?

I've been trying various combinations in the shell for the past 10 minutes, to no result.

Upvotes: 1

Views: 325

Answers (1)

Geo
Geo

Reputation: 96767

Ok, so here's how I finally did it:

class MyAdmin(admin.ModelAdmin):
  def get_form(self, request, obj=None, **kwargs):
    self.object_instance = obj
    return super(MyAdmin,self).get_form(request,obj,**kwargs)

After that, everything was easy.

Upvotes: 1

Related Questions