Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 '%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)
like image 701
Dennis Kioko Avatar asked Feb 01 '10 13:02

Dennis Kioko


People also ask

What does ForeignKey mean in Django?

ForeignKey is a Django ORM field-to-column mapping for creating and working with relationships between tables in relational databases.

What's the difference between Django OneToOneField and ForeignKey?

A one-to-one relationship. Conceptually, this is similar to a ForeignKey with unique=True , but the "reverse" side of the relation will directly return a single object. In contrast to the OneToOneField "reverse" relation, a ForeignKey "reverse" relation returns a QuerySet .

What does On_delete do on Django models?

The on_delete handle is used to handle the deletion of reference data to maintain the database integrity.

What is deferred field in Django?

Django's ORM provides a way to load only specific fields from the database using only and defer methods. The deferred fields are represented with the django. db. models. base.


2 Answers

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.

like image 107
Jack M. Avatar answered Oct 10 '22 09:10

Jack M.


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

like image 26
ayaz Avatar answered Oct 10 '22 08:10

ayaz