views:

375

answers:

2

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)
+2  A: 

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

ayaz
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) My problem is that memeberno will display "member Objects " in place of each object instead of the actual objects like "1", "2" , "3" etc.
gramware, you will need to define a `unicode(self)` method for the `Members` class as well.
ayaz
its already there 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() .... image = models.ImageField(max_length=100,upload_to='photos/%Y/%m/%d', blank=True, null=True) def __unicode__(self): return u'%s %s %s' % (self.fname, self.sname, self.memberno)
That is insanely hard to read, can you post that in your question?
Jack M.
+1  A: 

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.

Jack M.
Well, I have read somewhere that def __str__ is discouraged as it may result in some errors that cause Python to crash
Well if I change `__str__()` to return "FindMe", it shows up in the list of `Activity` object. If I change `__unicode__()` to "FindMe", it shows up in the drop down for other objects which have a `ForeignKey` to `Activity`. It may be discouraged, but it works.
Jack M.
I changed to def __str__(self): return "(%s)" % (self.memberno)but it still doesn't show the values
I edited my answer to reflect the lack of indention. In order to be a part of a class, the methods must be indented into the class' code block. Your `get_absolute_url` method is probably failing too.
Jack M.
I decided to abandon this class in favour of Django's built in user class. Thanks for your help