Meghdeep Ray
Meghdeep Ray

Reputation: 5537

Django : Type Mismatch Error in models.py

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 )

Upvotes: 1

Views: 597

Answers (4)

Mischback
Mischback

Reputation: 851

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)

Upvotes: 1

Sina Khelil
Sina Khelil

Reputation: 1991

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

Upvotes: 1

JuniorCompressor
JuniorCompressor

Reputation: 20015

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.

Upvotes: 2

Daniel Roseman
Daniel Roseman

Reputation: 599530

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)  

Upvotes: 6

Related Questions