Thursday, November 29, 2012

Re: admin foreign key issues



On Thu, Nov 29, 2012 at 9:10 AM, Larry Martell <larry.martell@gmail.com> wrote:
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'

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

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

No comments:

Post a Comment