Найти - Пользователи
Полная версия: как вывести в админке ForeignKey
Начало » Python для новичков » как вывести в админке ForeignKey
1
and_07
доброго времени суток
возникла проблема с выводом в админке
поле parent выводит как Section object
как вместо Section object вывести title?

model

class Section(models.Model):
    parent = models.ForeignKey('self',to_field='title', default=None, blank=True, null = True)
    title = models.CharField(max_length=100, unique=True)

admin

class SectionAdmin(admin.ModelAdmin):
    fields = ['parent','name','title', 'visible']
    list_display = ('name','title', 'visible')
admin.site.register(Section,SectionAdmin)   
 
and_07
img
mks
К модели Section добавте метод __unicode__ и возвращайте self.title, должно помочь.
and_07
class Section(models.Model):
    parent = models.ForeignKey('self',to_field='title', default=None, blank=True, null = True)
    title = models.CharField(max_length=100, unique=True)
    name = models.CharField(max_length=100, unique=True)
    def __unicode__(self):
        return u'%s - %s' % (self.title,self.name) 

Спасибо тема закрыта
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Powered by DjangoBB