开发者

Django-models ForeignKey Objects display Field Name instead of Object Values

I am using a ForeignKey called Memberno in My Django models. After entering data in the Members Class, it appears in the other classes as "member object" in the 'memberno' field. For ForeignKey, I am using 'raw_id_fields' as a workaround, but for normal fields and even 'filter_horizontal' , each memberno value is displayed as "member object". I would like the values to appear as they are in the member class i.e. 1, 2, ...

from django.db import models

# Create your models here.
class Members(models.Model):
    DESIGNATION_CHOICES=(
    ('ADM', 'Administrator'),
    ('OFF', 'Club Official'),
    ('MEM', 'Ordinary Member'),
    )
    memberno = models.AutoField(primary_key=True)
    fname = models.CharField(max_length=15)
    sname = models.CharField(max_length=15)
    onames = models.CharField(max_length=30, blank=True)
    email = models.EmailField()
    phoneNumber = models.CharField(max_length=15)
    regNo = models.CharField(max_length=15)
    designation = models.CharField(max_length=3,choices=DESIGNATION_CHOICES)
    image = models.ImageField(max_length=100,upload_to='photos/%Y/%m/%d', blank=True, null=True)
    course = models.CharField(max_length=30, blank=True, null=True)


def __unicode__(self):
    return  u'%s %s %s' % (self.fname, self.sname, self.memberno)


def get_absolute_url(self):
    return '%开发者_Python百科s%s/%s' % (settings.MEDIA_URL, settings.ATTACHMENT_FOLDER, self.id)

def get_download_url(self):
    return '%s%s/%s' % (settings.MEDIA_URL, settings.ATTACHMENT_FOLDER, self.name)




class Security(models.Model):
    memberno = models.ForeignKey(Members, unique=True)
    username = models.CharField(max_length=30, primary_key=True)
    password = models.CharField(max_length=70)

def __unicode__(self):
    return  u'%s %s %s' % (self.username, self.password, self.memberno)


class Subscriptions(models.Model):
    memberno = models.ForeignKey(Members)
    receiptNo = models.CharField(max_length=30, primary_key=True)
    type = models.CharField(max_length=50)
    date = models.DateField()
    amount = models.FloatField(max_length=99)

def __unicode__(self):
    return  u'%s %s %s' % (self.receiptno, self.type, self.memberno)




class events(models.Model):
    eventName  = models.CharField(max_length=25)
    eventID =  models.AutoField(primary_key=True)
    details = models.TextField()
    attendanceFee = models.FloatField(max_length=99)
    date = models.DateField()
    memberno = models.ManyToManyField(Members)



def __unicode__(self):
    return  u'%s %s %s' % (self.eventName, self.eventID, self.memberno)


The admin interface appears to use __str__() to determine the display of a model. I make it a habbit to always define both __unicode__() and __str__() when I create a model. For example:

class Activity(models.Model):
    activity_code = models.ForeignKey(ActivityCode)
    note = models.TextField(blank=True)
    def __str__(self):
        return "(%s) %s" % (self.activity_code.short, self.note)
    def __unicode__(self):
        return u'(%s) %s' % (self.activity_code.short, self.note)

Update:
It looks as though your def __unicode__(self) is not indented properly in your code. As a result it is not a part of your class, and is not being used. Indenting is very important in Python.


I am not sure I understand your question completely, but have you defined a unicode method for your models?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜