2

I'm writting a Django blog. I registered two models in Django admin (Category, Post). I wanted to add a new post with some category but I got following error:

Internal Server Error: /admin/blog/post/add/
Traceback (most recent call last):
  File "/home/pecan/env/lib/python3.4/site-packages/django/core/handlers/exception.py", line 41, in inner
    response = get_response(request)
  File "/home/pecan/env/lib/python3.4/site-packages/django/core/handlers/base.py", line 187, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/home/pecan/env/lib/python3.4/site-packages/django/core/handlers/base.py", line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/pecan/env/lib/python3.4/site-packages/django/contrib/admin/options.py", line 551, in wrapper
    return self.admin_site.admin_view(view)(*args, **kwargs)
  File "/home/pecan/env/lib/python3.4/site-packages/django/utils/decorators.py", line 149, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "/home/pecan/env/lib/python3.4/site-packages/django/views/decorators/cache.py", line 57, in _wrapped_view_func
    response = view_func(request, *args, **kwargs)
  File "/home/pecan/env/lib/python3.4/site-packages/django/contrib/admin/sites.py", line 224, in inner
    return view(request, *args, **kwargs)
  File "/home/pecan/env/lib/python3.4/site-packages/django/contrib/admin/options.py", line 1508, in add_view
    return self.changeform_view(request, None, form_url, extra_context)
  File "/home/pecan/env/lib/python3.4/site-packages/django/utils/decorators.py", line 67, in _wrapper
    return bound_func(*args, **kwargs)
  File "/home/pecan/env/lib/python3.4/site-packages/django/utils/decorators.py", line 149, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "/home/pecan/env/lib/python3.4/site-packages/django/utils/decorators.py", line 63, in bound_func
    return func.__get__(self, type(self))(*args2, **kwargs2)
  File "/home/pecan/env/lib/python3.4/site-packages/django/contrib/admin/options.py", line 1408, in changeform_view
    return self._changeform_view(request, object_id, form_url, extra_context)
  File "/home/pecan/env/lib/python3.4/site-packages/django/contrib/admin/options.py", line 1440, in _changeform_view
    if form.is_valid():
  File "/home/pecan/env/lib/python3.4/site-packages/django/forms/forms.py", line 183, in is_valid
    return self.is_bound and not self.errors
  File "/home/pecan/env/lib/python3.4/site-packages/django/forms/forms.py", line 175, in errors
    self.full_clean()
  File "/home/pecan/env/lib/python3.4/site-packages/django/forms/forms.py", line 386, in full_clean
    self._post_clean()
  File "/home/pecan/env/lib/python3.4/site-packages/django/forms/models.py", line 414, in _post_clean
    self.validate_unique()
  File "/home/pecan/env/lib/python3.4/site-packages/django/forms/models.py", line 423, in validate_unique
    self.instance.validate_unique(exclude=exclude)
  File "/home/pecan/env/lib/python3.4/site-packages/django/db/models/base.py", line 1034, in validate_unique
    date_errors = self._perform_date_checks(date_checks)
  File "/home/pecan/env/lib/python3.4/site-packages/django/db/models/base.py", line 1145, in _perform_date_checks
    date = getattr(self, unique_for)
TypeError: getattr(): attribute name must be string
[21/Sep/2017 22:16:01] "POST /admin/blog/post/add/ HTTP/1.1" 500 143101
[21/Sep/2017 22:16:16] "GET /admin/blog/post/add/ HTTP/1.1" 200 7865
[21/Sep/2017 22:16:48] "GET /static/admin/img/calendar-icons.svg HTTP/1.1" 200 1094

What's I am doing wrong?

Here's file models.py:

from django.contrib.auth.admin import User
from django.db import models
from django.utils import timezone


# Create your models here.
class Category(models.Model):
    name = models.CharField(max_length=50)
    description = models.TextField(blank=True)


class Post(models.Model):
    title = models.CharField(max_length=250)
    author = models.ForeignKey(User, on_delete=models.CASCADE, related_name="post_author")
    slug = models.SlugField(unique_for_date=True)
    content = models.TextField(blank=True)
    created = models.DateField(default=timezone.now)
    modified = models.DateField(auto_now_add=True)
    published = models.DateField(auto_now=True)
    category = models.ForeignKey(Category, on_delete=models.CASCADE, related_name="post_category")

and admin.py:

from django.contrib import admin
from . import models

# Register your models here.
admin.site.register(models.Post)
admin.site.register(models.Category)

Hope for help!

1 Answer 1

3

you need set column name(by your logic), for example created

 slug = models.SlugField(unique_for_date='created')

more details unique-for-date

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

3 Comments

Thank you very much! Additional question: why Django admin shows Post object instead of values of Post object attributes?
sorry but i don't use django admin, don't know it
override your str method for Post like def str(self): return self.title will shows Post title in admin instead of object

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.