python - Django: How to display foreign key value in admin form -
how admin form display foreign key value in drop down list of django admin form rather display 'table name object'? struggling work out.
here examples of model code (both contain catname field. catname pk in parent , fk in child).
parent(models.model): catname = models.charfield(db_column='catname', primary_key=true, max_length=255, verbose_name="catname") description = models.charfield(db_column='description', max_length=255, blank=true, null=true) class meta: managed = false db_table = 'parent' verbose_name_plural = ('parent') #def __unicode__(self): return self.catname.catname def __str__(self): return '%s' % (self.catname) class child(models.model): description = models.charfield(db_column='vardescription', max_length=255, blank=true, null=true) cname = models.charfield(db_column='varname', primary_key=true, max_length=255) catname = models.foreignkey(parent, db_column='catname', blank=true, null=true) def __unicode__(self): return self.name def child_cat(self): return self.catname.catname class meta: managed = false db_table = 'child' verbose_name_plural = ('child')
admin looks like:
from django.contrib import admin library.models import parent, child # register models here. class childadmin(admin.modeladmin): list_display = ('cname','description','child_cat') fields=('cname','description','child_cat') search_fields = ('cname','description') ordering = ('cname',) admin.site.register(child,childadmin)
you can override form
used modeladmin
or change 1 field modeladmin.formfield_overrides
.
read documentation here
Comments
Post a Comment