Django error value too long for type character varying(50)
I get the error below when I try to add a new article to my site
value too long for type character varying(50)
Request Method: POST
Django Version: 4.0.4
Exception Type: DataError
Exception Value:
value too long for type character varying(50)
Exception Location: /opt/venv/lib/python3.8/site-packages/django/db/backends/utils.py, line 89, in _execute
Python Executable: /opt/venv/bin/python
Python Version: 3.8.16
Python Path:
['/app',
'/opt/venv/bin',
'/nix/store/9493ngfjwisjm5f5s5ld5m34rdy431ic-python3-3.8.16/lib/python38.zip',
'/nix/store/9493ngfjwisjm5f5s5ld5m34rdy431ic-python3-3.8.16/lib/python3.8',
'/nix/store/9493ngfjwisjm5f5s5ld5m34rdy431ic-python3-3.8.16/lib/python3.8/lib-dynload',
'/opt/venv/lib/python3.8/site-packages',
'/opt/venv/lib/python3.8/site-packages/odf',
'/opt/venv/lib/python3.8/site-packages/odf',
'/opt/venv/lib/python3.8/site-packages/odf',
'/opt/venv/lib/python3.8/site-packages/odf',
'/opt/venv/lib/python3.8/site-packages/odf',
'/opt/venv/lib/python3.8/site-packages/odf',
'/opt/venv/lib/python3.8/site-packages/odf']
Server time: Sun, 23 Apr 2023 10:10:10 +0000
Here is my models.py file content
class EnglishArticle(models.Model):
author = models.ForeignKey(Author, related_name='author',on_delete=models.SET_NULL, null=True)
slug = models.SlugField()
title = models.CharField(max_length=400)
body = RichTextUploadingField(default='Empty Content')
date_created = models.DateField()
published = models.BooleanField(default=False)
image = models.ImageField(blank = True, null = True)
reading_time = models.IntegerField()
views = models.IntegerField(default=0)
class Meta:
verbose_name_plural = 'Articles'
def __str__(self):
return self.title
def save(self, *args, **kwargs):
title = self.title.lower()
x = ['@', '#', '?', "/", ".", ",", "&", "!","|","-","_"]
z ="".join(filter(lambda char: char not in x, title))
self.slug = re.sub(r"\s+", '-', z)
super().save(*args, **kwargs)
class FrenchArticle(models.Model):
title = models.CharField(max_length=400)
body = RichTextUploadingField(default='Empty Content')
french_article = models.OneToOneField(
EnglishArticle,
related_name="french_article",
blank=True,
null=True,
on_delete=models.CASCADE)
class Meta:
verbose_name_plural = 'French Articles'
def __str__(self):
return self.title
Admin
2023-04-23
Django by default assigns a max length of 50 characters to a slugfield, if you will like to add a slug with higher number of characters then you have to specify a max length for thr slugfield as shown below
slug = models.SlugField(max_length=400)
Similar Content
- Error: Invalid src prop (https://res.cloudinary.com) on next/image
- How to access inputed values of model fields before creation in Django admin dashboard
- How to change project name on Django
- flask error: flask.cli.NoAppException: Could not import "app name".
- Part 5: Building a User-Friendly Todo App with Flask - Step-by-Step Guide to Update Todo and Category Items via PATCH Requests
Privacy Policy
By using our website,
you agree that devmaesters can store cookies on your device and disclose information in accordance with our privacy policy.