Django admin: naming subclass objects, fieldsets and displaying items from other class.

205 views
Skip to first unread message

Petey

unread,
Jul 17, 2011, 5:05:44 AM7/17/11
to django...@googlegroups.com
Hi

1. When in django.admin I want to pick a source from dropdown list it always lists every item as "source object".
How I can modify it so it will show source_name from Source model?

from django.contrib import admin
from news.models import News, Comment, Source

class NewsAdmin(admin.ModelAdmin):
    date_hierarchy = 'date_added'
    list_display = ('date_added',  'title', 'created_by')
    list_display_links = ('date_added', 'title', 'created_by', )
    fields = ['title', 'text', "source",  'created_by']

class SourceAdmin(admin.ModelAdmin):
    list_display = ('source_name', 'source_url')

Models:
class News(models.Model):
    source = models.ForeignKey("Source", related_name="source_name_set", blank=True, null=True)

class Source(models.Model):
    source_name = models.CharField(max_length=50)
    source_url = models.URLField("Link")


2. I am having a problem with fieldsets in django admin, second parameter "advanced options does not work for any subclass of models.

It always returns me an error: 'NewsAdmin.fieldsets[1][1]['fields']' refers to field 't' that is missing from the form.
where field "x" is always the first letter of particural model subclass for exmaple "s" - source, "t" title.

fieldsets = (
        (None, {
            'fields': ('text')
        }),
        ('Advanced options', {
            'classes': ('collapse',),
            'fields': ('title')
        }),
    )

3. In list display in admin.model I'd like to view connected data (foreign key) from another model.

For example I have a CommentAdmin class and I want to see the title from NewsAdmin class and link it to particural news.

I have completely no idea how to do it - I couldn't find any examples in django docs

class CommentAdmin(admin.ModelAdmin):
    date_hierarchy = 'date_added'
    list_display = ('id', 'date_added', 'title', )

class NewsAdmin(admin.ModelAdmin):
    date_hierarchy = 'date_added'
    list_display = ('date_added',  'title', 'created_by')
    list_display_links = ('date_added', 'title', 'created_by', )
    fields = ['title', 'text', "source",  'created_by']



Please provide me some hints and help with these 3 issues and I will try to resolve the problems :)
I will appreciate it

Martin J. Laubach

unread,
Jul 17, 2011, 5:29:46 AM7/17/11
to django...@googlegroups.com
1. Define a unicode method on Source that returns whatever you want to display.
2. Missing comma in one-element tuple.

        mjl

Petey

unread,
Jul 17, 2011, 5:49:37 AM7/17/11
to django...@googlegroups.com
Both helped.

3rd problem to go ;)

Daniel Roseman

unread,
Jul 17, 2011, 6:31:40 AM7/17/11
to django...@googlegroups.com
On Sunday, 17 July 2011 10:49:37 UTC+1, Petey wrote:
Both helped.

3rd problem to go ;)

--
DR. 
Reply all
Reply to author
Forward
0 new messages