Karen thank you very much for your accurate reply.

>  Changing the display in the
> change view is a little more involved -- that needs to change the form used
> by the admin to one where the field in question has a customized
> label_from_instance that returns what you are looking for.
>

This is the solution I found to the problem, following your advice (in
case sb else is looking for this also):

file: admin.py

from django.contrib import admin
from django.forms import ModelForm, ModelChoiceField

from myapp.models import Model1,  Model2

class Model1Admin(admin.ModelAdmin):
    pass

class MyModelChoiceField(ModelChoiceField):
    def label_from_instance(self, obj):
        return '%s' % obj.code2

class MyModel2AdminForm(ModelForm):
    mycode = MyModelChoiceField(Model1.objects) #this is not well
documented imho

    class Meta:
        model = Model2

class Model2Admin(admin.ModelAdmin):
    form = MyModel2AdminForm
    def mycode_display(self, m2obj):
        return m2obj.mycode.code2
    list_display = ['mycode_display', 'mydata']

admin.site.register(Model1, Model1Admin)
admin.site.register(Model2, Model2Admin)

I think the overriding of a ModelChoiceField in a ModelForm should
have an example in the documentation (e.g. at
http://docs.djangoproject.com/en/dev/topics/forms/modelforms/#overriding-the-default-field-types).
I figured out that it needs a Model1.objects argument because the
exceptions mentioned that "type object 'Model1' has no attribute 'all'
".

--

You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.


Reply via email to