admin foreign key issues

18 views
Skip to first unread message

Larry Martell

unread,
Nov 29, 2012, 9:10:49 AM11/29/12
to django...@googlegroups.com
This is probably very simple, but I've never run into this before and
googling has not revealed anything.

I have these 2 models:

class Category(models.Model):
class Meta: db_table = 'data_category'
name = models.CharField(max_length=20, unique=True, db_index=True)

class Tool(models.Model):
class Meta: db_table = 'data_tool'
name = models.CharField(max_length=10, unique=True)
category = models.ForeignKey(Category)

In admin when I click on 'Categorys' I get a table of 'Category
object' and I have to click on those to get at an actual row in the
table. How can I make the data available from 'Categorys' without that
extra level?

Similarly, in Tools the Category column has 'Category object' and when
I click on a specific tool and get into change tool, the Category is a
drop down that has 'Category object' as every choice. How can I make
the actual category names appear here?

I tried defining a CategoryAdmin class:

class CategoryAdmin(admin.ModelAdmin):
list_display = ('name')
list_filter = ('name')
admin.site.register(Category, CategoryAdmin)

But that fails with 'CategoryAdmin.list_display' must be a list or tuple.


And finally, how can I get it to display 'Categories' instead of 'Categorys'

TIA!
-larry

Bill Freeman

unread,
Nov 29, 2012, 10:43:58 AM11/29/12
to django...@googlegroups.com
Start by creating a __unicode__() method on each of your models that returns a string giving a better description of your object, say by using its 'name' field.  If you are using python3, then see the recent discussion here about what to do instead of __unicode__().

Use of the __unicode__() method is described in the tutorial.

Bill

Larry Martell

unread,
Nov 29, 2012, 12:30:46 PM11/29/12
to django...@googlegroups.com
Thanks much Bill, that did the trick.

Now does anyone know how to get it to display 'Categories' instead of
'Categorys'?

Bill Freeman

unread,
Nov 29, 2012, 12:34:21 PM11/29/12
to django...@googlegroups.com

Larry Martell

unread,
Nov 29, 2012, 12:36:44 PM11/29/12
to django...@googlegroups.com
Thanks again, Bill.

Chris Cogdon

unread,
Nov 30, 2012, 6:18:40 PM11/30/12
to django...@googlegroups.com



class CategoryAdmin(admin.ModelAdmin):
    list_display = ('name')
    list_filter = ('name')
admin.site.register(Category, CategoryAdmin)

But that fails with 'CategoryAdmin.list_display' must be a list or tuple.

use:   list_display = ('name',)

Note the trailing comma, there?

In python, a tuple with a single element must use the form ( x, )  or it is indistinguishable from a parenthesised expression. 
 

Reply all
Reply to author
Forward
0 new messages