Jump to content

[django] Aparecer campo específico numa combobox relacional


HecKel
 Share

Recommended Posts

Boas!

A pergunta é simples, eu tenho uma BD com algumas tabelas, e estou agora a criar o painel de admin de uma das tabelas, acontece que que uma das opções, chave externa, aparece como combobox (exactamente como eu queria), no entanto eu gostava de saber como faço para eu escolher qual o campo da outra tabela a ser mostrado em vez de aparecer City object como aparece actualmente, é que se eu tiver 10 cidades, irá aparecer-me 10 City objects, não dá para diferenciar por este texto.

Alguém tem ideia de como fazer isto?

Link to comment
Share on other sites

pá, eu estou a seguir o tutorial do django, apenas "chamo" a classe.

mas aqui fica o meu admin.py:

from lafora.forum.models import Country, City, Member, Board
from django.contrib import admin

class CityInline(admin.StackedInline):
model = City
extra = 1

class CountryAdmin(admin.ModelAdmin):
fieldsets = [
	(None,               {'fields': ['name']}),
]
inlines = [CityInline]
search_fields = ['name']
#list_display = ('name', None)

class BoardAdmin(admin.ModelAdmin):
fieldsets = [
	(None,       {'fields': ['name']}),
	('Position', {'fields': ['parent_board', 'position'], 'classes': ['collapse']}),
]
search_fields = ['name']

class MemberAdmin(admin.ModelAdmin):
fieldsets = [
	(None,               {'fields': ['displayname']}),
	('Data Information', {'fields': ['username', 'email', 'bdate', 'regdat', 'active', 'city'], 'classes': ['collapse']}),
]
list_display = ('displayname', 'email', 'city', 'active')
list_filter = ['regdat', 'city']
search_fields = ['username', 'displayname', 'email', 'city', 'active']
date_hierarchy = 'regdat'

admin.site.register(Member, MemberAdmin)
admin.site.register(Country, CountryAdmin)
admin.site.register(Board, BoardAdmin)

Já agora, o meu models.py:

from django.db import models

# Create your models here.
class Country(models.Model):
name = models.CharField(max_length=60)

class City(models.Model):
name = models.CharField(max_length=60)
country = models.ForeignKey(Country)

class Member(models.Model):
username = models.CharField(max_length=30)
displayname = models.CharField(max_length=60)
password = models.CharField(max_length=40)
email = models.EmailField()
bdate = models.DateField()
regdat = models.DateTimeField()
active = models.CharField(max_length=32)
city = models.ForeignKey(City)

class Board(models.Model):
name = models.CharField(max_length=30)
parent_board = models.IntegerField() # pensar numa forma de fazer aqui uma relacao
position = models.IntegerField()

class Topic(models.Model):
title = models.CharField(max_length=60)
visualizations = models.IntegerField()
member = models.ForeignKey(Member)
board = models.ForeignKey(Board)

class Message(models.Model):
date = models.DateTimeField()
text = models.TextField()
parent_message = models.IntegerField()
member = models.ForeignKey(Member)
topic = models.ForeignKey(Topic)
Link to comment
Share on other sites

RTFM 🙂 É o que te tenho a dizer...

Do manual do django:

Wait a minute. <Poll: Poll object> is, utterly, an unhelpful representation of this object. Let's fix that by editing the polls model (in the polls/models.py file) and adding a __unicode__() method to both Poll and Choice:

Basicamente, tens que alterar a tua classe City para:

class City(models.Model):

name = models.CharField(max_length=60)

country = models.ForeignKey(Country)

def __unicode__(self):
    return self.name

While you are at it... muda para todas a que precisas de retornar valores 😛

Ah, e usa 4 espaços em vez de um tab 😛

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.