Reputation: 23
In a normal python program, I understand what this problem means but I am not able to find the reason behind this error while saving posts from admin in Django.
Have I given any invalid data according to field?
So while saving it gives the following error:
Environment:
Request Method: POST
Request URL: http://localhost:8000/admin/blog/post/add/
Django Version: 3.2.4
Python Version: 3.6.9
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'blog']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback (most recent call last):
File "/home/prython/2021/Django_thrpracticalGuide/venv/lib/python3.6/site-packages/django/core/handlers/exception.py", line 47, in inner
response = get_response(request)
File "/home/prython/2021/Django_thrpracticalGuide/venv/lib/python3.6/site-packages/django/core/handlers/base.py", line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/prython/2021/Django_thrpracticalGuide/venv/lib/python3.6/site-packages/django/contrib/admin/options.py", line 616, in wrapper
return self.admin_site.admin_view(view)(*args, **kwargs)
File "/home/prython/2021/Django_thrpracticalGuide/venv/lib/python3.6/site-packages/django/utils/decorators.py", line 130, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "/home/prython/2021/Django_thrpracticalGuide/venv/lib/python3.6/site-packages/django/views/decorators/cache.py", line 44, in _wrapped_view_func
response = view_func(request, *args, **kwargs)
File "/home/prython/2021/Django_thrpracticalGuide/venv/lib/python3.6/site-packages/django/contrib/admin/sites.py", line 232, in inner
return view(request, *args, **kwargs)
File "/home/prython/2021/Django_thrpracticalGuide/venv/lib/python3.6/site-packages/django/contrib/admin/options.py", line 1657, in add_view
return self.changeform_view(request, None, form_url, extra_context)
File "/home/prython/2021/Django_thrpracticalGuide/venv/lib/python3.6/site-packages/django/utils/decorators.py", line 43, in _wrapper
return bound_method(*args, **kwargs)
File "/home/prython/2021/Django_thrpracticalGuide/venv/lib/python3.6/site-packages/django/utils/decorators.py", line 130, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "/home/prython/2021/Django_thrpracticalGuide/venv/lib/python3.6/site-packages/django/contrib/admin/options.py", line 1540, in changeform_view
return self._changeform_view(request, object_id, form_url, extra_context)
File "/home/prython/2021/Django_thrpracticalGuide/venv/lib/python3.6/site-packages/django/contrib/admin/options.py", line 1579, in _changeform_view
form_validated = form.is_valid()
File "/home/prython/2021/Django_thrpracticalGuide/venv/lib/python3.6/site-packages/django/forms/forms.py", line 175, in is_valid
return self.is_bound and not self.errors
File "/home/prython/2021/Django_thrpracticalGuide/venv/lib/python3.6/site-packages/django/forms/forms.py", line 170, in errors
self.full_clean()
File "/home/prython/2021/Django_thrpracticalGuide/venv/lib/python3.6/site-packages/django/forms/forms.py", line 374, in full_clean
self._post_clean()
File "/home/prython/2021/Django_thrpracticalGuide/venv/lib/python3.6/site-packages/django/forms/models.py", line 413, in _post_clean
self.instance.full_clean(exclude=exclude, validate_unique=False)
File "/home/prython/2021/Django_thrpracticalGuide/venv/lib/python3.6/site-packages/django/db/models/base.py", line 1216, in full_clean
self.clean_fields(exclude=exclude)
File "/home/prython/2021/Django_thrpracticalGuide/venv/lib/python3.6/site-packages/django/db/models/base.py", line 1258, in clean_fields
setattr(self, f.attname, f.clean(raw_value, self))
File "/home/prython/2021/Django_thrpracticalGuide/venv/lib/python3.6/site-packages/django/db/models/fields/__init__.py", line 671, in clean
self.run_validators(value)
File "/home/prython/2021/Django_thrpracticalGuide/venv/lib/python3.6/site-packages/django/db/models/fields/__init__.py", line 623, in run_validators
v(value)
File "/home/prython/2021/Django_thrpracticalGuide/venv/lib/python3.6/site-packages/django/core/validators.py", line 358, in __call__
if self.compare(cleaned, limit_value):
File "/home/prython/2021/Django_thrpracticalGuide/venv/lib/python3.6/site-packages/django/core/validators.py", line 392, in compare
return a < b
Exception Type: TypeError at /admin/blog/post/add/
Exception Value: '<' not supported between instances of 'str' and 'int'
Here is how my models.py file look like:
from django.core import validators
from django.db import models
from django.utils.text import slugify
from django.core.validators import MinValueValidator
# Create your models here.
class Tag(models.Model):
caption = models.CharField(max_length=20)
def __str__(self):
return self.caption
class Author(models.Model):
first_name = models.CharField(max_length=100)
last_name = models.CharField(max_length=100)
email_address = models.EmailField()
def full_name(self):
return f"{self.first_name} {self.last_name}"
def __str__(self):
return self.full_name()
class Post(models.Model):
title = models.CharField(max_length=100)
excrept = models.CharField(max_length=150)
image_name = models.CharField(max_length=150)
date = models.DateField(auto_now=True)
slug = models.SlugField(unique=True)
content = models.TextField(validators=[MinValueValidator(10)])
author = models.ForeignKey(Author, on_delete=models.SET_NULL, null=True, related_name='posts')
tags = models.ManyToManyField(Tag)
def save(self, *args, **kwargs):
self.slug = slugify(self.title)
super().save(*args, **kwargs)
def __str__(self):
return self.title
Let me know if I need to send anyother file?
Upvotes: 0
Views: 661
Reputation: 2380
for TextField
s you should use MinLengthValidator
.
from django.core.validators import MinLengthValidator
class Post(models.Model):
# ...
content = models.TextField(validators=[MinLengthValidator(10)])
# ...
Upvotes: 2