1

When I execulte these lines in python3 manage.py shell :

In [1]: from dream_site.models import Mentor, Mentee, Team
In [2]: a = Mentee.objects.get( pk = 1 ).name
In [3]: b = Mentor.objects.get(pk=1).name
In [4]: print( a + b )

It prints the proper output as expected, however in the models.py when I have the following code it gives me the int() argument must be a string or a number error :

def __str__(self):     # Belonging to Team object
    return ( str( self.pk ) + " - " +  Mentor.objects.get( pk = self.mentor_id ).name   + " - " +  Mentee.objects.get( pk = self.mentee_id ).name   )

This is the place where the error is occurring. Even though it shows these as str types in the shell.
Other parts of the models.py :

class Mentor(models.Model):
    mentor_id =     models.AutoField( primary_key = True )
    name =          models.CharField( default = '', max_length = 254 )

class Mentee(models.Model):
    mentee_id =     models.AutoField( primary_key = True )
    name =          models.CharField( default = '', max_length = 254 )

class Team(models.Model):
    team_id =                       models.AutoField( primary_key = True )
    mentor_id =                     models.ForeignKey( Mentor )
    mentee_id =                     models.ForeignKey( Mentee )
    team_since =                    models.DateTimeField( default = django.utils.timezone.now, blank = True )
2
  • 2
    of which class is __str__(self) part of? Commented Jul 25, 2015 at 19:08
  • Sorry I forgot to specify that it was in the Team object. Commented Jul 25, 2015 at 19:09

4 Answers 4

6

You've confused yourself by using the wrong naming convention.

In Team, mentor_id and mentee_id are not IDs. They are are the actual Mentor and Mentee objects. So in your __str__ method, where you try and query the Mentee class using the value of mentee_id, you're actually passing an existing Mentee object to the query. There's no need to do that.

Your fields should be called mentor and mentee, and you should use them directly in your method:

return str(self.pk) + " - " +  self.mentor.name   + " - " +  self.mentee.name

or even better, use string interpolation:

return "%s - %s - %s" %  (self.pk, self.mentor.name, self.mentee.name)  
Sign up to request clarification or add additional context in comments.

1 Comment

Daniel, it would have to be self.mentor_id and self.mentee_id (since they were named that way) it would have been best oif they were named without the _id since in the db, the '_id' is added by django when the tables are created
2

mentor_id, mentee_id are not integers in Team class. They are of Mentor and Mentee type respectively. Use instead:

 self.mentor_id.name

and

 self.mentee_id.name

in __str__.

Of course it's even better to rename these fields to mentor and mentee in the Team class.

1 Comment

Thank you ! Don't know why you got downvoted randomly though.
1

It's a foreign key, you can access it's table elements directly. Also, use format, it does the type conversion on your behalf.

def __str__(self):     # Belonging to Team object
    return "{0} - {1} - {2}".format(self.pk, self.mentor_id.name, self.mentee_id.name)

For more details on formatting, check out PyFormat

Comments

1

You should just reference them as objects...

class Team(models.Model):
    team_id =                       models.AutoField( primary_key = True )
    mentor_id =                     models.ForeignKey( Mentor )
    mentee_id =                     models.ForeignKey( Mentee )
    team_since =                    models.DateTimeField( default = django.utils.timezone.now, blank = True )

    def __str__(self):
        return '{0} - {1} - {2}'.format(self.team_id, self.mentor_id.name, self.mentee_id.name)

Django provides you with an ORM wrapper, which means, you can build your classes and especially the connection between your classes, without caring about database storage or implementation. You can easily assume, that your Team objects have a direct link to their corresponding Mentor and Mentee objects. You should not care about database id's in Django code.

So, even better (clearer) would be this:

class Team(models.Model):
    team_id = models.AutoField( primary_key = True )
    mentor = models.ForeignKey( Mentor )
    mentee = models.ForeignKey( Mentee )
    team_since = models.DateTimeField(default=django.utils.timezone.now, blank=True )

    def __str__(self):
        return '{0} - {1} - {2}'.format(self.team_id, self.mentor.name, self.mentee.name)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.