Hey readers,
I wanted to share with you an issue that I ran into today and how I solved it.
Problem:
I'm working on creating a new model in my django app. All the fields were created, and I wanted my app to sort the model by the 'name' field. Provided below is my code:
from django.db import models
from django.urls import reverse # Used to generate URLs by reversing the URL patterns
import uuid # Required for unique book instances
class Stock(models.Model):
"""Model representing a user's selected stock
Args:
models (_type_): _description_
"""
id = models.UUIDField(primary_key=True, default=uuid.uuid4),
name = models.TextField('Stock Name', max_length=1000, null=False),
symbol = models.TextField('Ticker Symbol', max_length=100, null=False),
tags = models.TextField('Search Tags', max_length=1000, help_text="Enter additional keywords to use in the search ('#XBox', 'IOS')"),
active=models.BooleanField('Activate or Deactivate', default=True, help_text="Check to activate the stock or uncheck to deactivate the stock."),
def __str__(self):
return f'{self.name} - {self.symbol}'
class Meta:
ordering=['-name']
def get_absolute_url(self):
"""Returns the URL to access a detail record for this stock."""
return reverse('stock-detail', args=[str(self.id)])
Once I was done with developing the model, I saved my changes and started the migration process.
In my terminal I typed in the following command:
python3 manage.py makemigrations
Once I hit enter I got the following error:
_SystemCheckError: System check identified some issues:
ERRORS:
StockTwitterApp.Stock: (models.E015) 'ordering' refers to the nonexistent field, related field, or lookup 'name'._
Next I went to google to see if anyone had experience a similar issue. I went through a couple of stackoverflow results and nothing panned out.
Next I decided to compare my code with the tutorial's example I was using to see if I had missed anything or added anything additional that was not needed. And then I found it.....
Solution:
It was was the commas ',' . The commas at the end of each field definition (see below):
id = models.UUIDField(primary_key=True, default=uuid.uuid4)->,<-
Once I had removed the commas from the end of each field definition, saved my changes, and then reran the makemigrations command (python3 manage.py makemigrations)....boom!!! The creation of the migration was a success.
What did I learn:
Do not put commas where they do not belong :)
I hope this was helpful.
Please feel free to leave any feedback and\or questions you may have.
Thanks for reading and have a wonderful day.
Top comments (0)