2017-09-21 61 views
1

私はDjangoのブログを書いています。私はDjango admin(CategoryPost)に2つのモデルを登録しました。私はいくつかのカテゴリに新しいポストを追加したいが、私は次のエラーました:私は間違って何をやっていますTypeError:getattr():属性名はDjangoの文字列でなければなりません

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 

を?

ここでファイル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") 

admin.py

from django.contrib import admin 
from . import models 

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

希望の助けを!あなたは(あなたのロジックによる)セットのカラム名を必要とする

答えて

1

は、例えばcreated

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

詳細unique-for-date

+0

はどうもありがとうございました!追加の質問:なぜDjangoの管理者はPostオブジェクトの属性の値の代わりにPostオブジェクトを表示するのですか? –

+0

申し訳ありませんが、私はdjangoの管理者を使用していません。それはわかりません –

+0

すごくご迷惑をおかけして申し訳ありませんが、私はDjangoの学習を始めました。 –

関連する問題