0

I have the following code and an error that says 'inconsistent use of spaces and indentation' but I cannot see what has been done wrong?

class Song(models.Model):
    album=models.ForeignKey(Album, on_delete=models.CASCADE)
    file_type=models.CharField(max_length=10)
    song_title=models.CharField(max_length=250)

    def __str__(self):
        return self.song_title

The code above it for another class is exactly the same, but it works

class Album(models.Model):
    artist=models.CharField(max_length=250)
    album_title=models.CharField(max_length=500)
    genre=models.CharField(max_length=100)
    album_logo=models.CharField(max_length=1000)

    def __str__(self): #this is a string representation of this object - useful for testing/viewing purposes
        return self.album_title+""+self.artist

The error appears to be in the line that returns the string representation of the Song object

def __str__(self):
            return self.song_title

More specifically the error seems to point to this line:

def __str__(self): (if you note its position above int he Song class, the indentation looks fine...)

Can anyone spot the error or suggest a fix?

4
  • Make use of your editor or IDE, or better yet use one that has a tab to spaces conversion feature. Commented Dec 22, 2017 at 21:43
  • Well in the first example you got 1 tab and in your second example, you got two. If you have two tabs in the original code, indent it backward. Commented Dec 22, 2017 at 21:43
  • I've just cut and paste the first one into the second, so can't see how the indentation error arises. What do you mean by indent it backward? I tried that but nothing seems to work Commented Dec 22, 2017 at 21:45
  • fixed it by opening it in a Python File (IDLE) instead of notepad. Makes no sense, but it worked! Commented Dec 22, 2017 at 21:49

1 Answer 1

2

This error occurs when you use the 'tab' button to indent your code in some places, but spacing in other. You just need to make sure you use either tabs or spaces throughout the whole file.

Find out which you use most often then adjust the outlier to the same method.

Sign up to request clarification or add additional context in comments.

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.