2017-05-23 25 views
1

モデルAccountのタイムスタンプを作成しようとしていますが、2つのタイムスタンプ(created_atmodified_at)ユーザが見ることができる。 created_atmodified_atフィールドにeditable=Falseを追加するまで、すべて正常に動作します。私は両方からeditable=Falseを削除するとすぐにFieldError:アカウントに指定された未知のフィールド(created_at、modified_at)

django.core.exception.FieldError: Unknown field(s) (created_at, modified_at) specified for Account

:私は何もやるしようとすると、ここで

class Account(models.Model): 
    account_name = models.CharField(max_length=25) 
    active = models.BooleanField(default=True) 
    created_at = models.DateTimeField(null=True, blank=True, editable=False) 
    modified_at = models.DateTimeField(null=True, blank=True, editable=False) 

    def save(self): 
     if self.id: 
      self.modified_at = datetime.datetime.now() 
     else: 
      self.created_at = datetime.datetime.now() 
     super(Account, self).save() 

    class Meta: 
     ordering = ('id',) 

は、私が手にあいまいなエラーである(移行し、そのrunserver、など):ここに私のモデルであり、フィールドは、すべて正常に動作します。これはDjangoのバグですか?フィールドを非表示にし、ユーザーが編集できないようにするには良い方法はありますか?

私はDjango 1.9とPython 3.6.1を使用しています。助けてくれてありがとう、何か他のもの(ビュー、シリアライザなど)を投稿する必要があるかどうかを教えてください。

EDIT

完全トレースバック:https://pastebin.com/YEQACX5z

アカウントフォーム:

class AccountForm(ModelForm): 
    class Meta: 
     model = Account 
     fields = ['account_name', 'active', 'created_at', 'modified_at'] 
+0

完全なトレースバックを表示してください。 – Alasdair

+0

フルトレースバックを追加しました。 – Logicman

+0

トレースバックは、表示されていない「AccountForm」でエラーが発生していることを示しています。 – Alasdair

答えて

2

あなただけ行うことができ、

created_at = models.DateTimeField(auto_now_add=True) 

01ドキュメントから
modified_at = models.DateTimeField(auto_now=True) 

DateField.auto_now¶

Automatically set the field to now every time the object is saved. Useful for “last-modified” timestamps. Note that the current date is always used; it’s not just a default value that you can override.

The field is only automatically updated when calling Model.save(). The field isn’t updated when making updates to other fields in other ways such as QuerySet.update(), though you can specify a custom value for the field in an update like that.

DateField.auto_now_add¶

Automatically set the field to now when the object is first created. Useful for creation of timestamps. Note that the current date is always used; it’s not just a default value that you can override. So even if you set a value for this field when creating the object, it will be ignored.

ので、偽=編集可能な追加そのすでに編集不可能にする必要はありません。

また、これらのフィールドを変更しようとしているので、save()メソッドオーバーライドを削除することを忘れないでください。

あなたはこのフィールドを変更できるようにしたい場合は、以下の代わりのauto_now_add =真を設定します。

For DateField: default=date.today - from datetime.date.today() 
For DateTimeField: default=timezone.now - from django.utils.timezone.now() 

このフィールドのデフォルトのフォームウィジェットがTextInputのです。管理者は、JavaScriptカレンダーと「今日」のショートカットを追加します。追加のinvalid_dateエラーメッセージキーが含まれています。

+0

これらの両方とも、editable = Falseを追加したのと同じエラーが表示されます: 'django.core.exception.FieldError:アカウント' – Logicman

+0

のUnknownフィールド(created_at、modified_at)が指定されています。 ) 'メソッドオーバーライド。 – Soviut

+0

あなたはあなたが探していたことを願っています! – zaidfazil

関連する問題