2015-08-28 18 views
-1

在Django中的新的,我試圖創建一個窗體爲我的應用程序添加書籍。但我希望發佈日期不包含在表單中。相反,我希望獲得當前的系統日期,並將「添加」表單以保存我的模型。我怎麼能這樣做?如何在Django Forms中自動添加實際日期?

還有就是我views.py部分:

def add_book(request): 
if request.method == 'POST': 
    form = BookForm(request.POST) 
    if form.is_valid(): 
     new_book = form.save(commit=False) 
     new_book.publication_date = django_timezone 
     new_book.save() 
     return HttpResponseRedirect('/thanks/') 
    else: 
     print form.errors 
else: 
    form = BookForm() 
return render_to_response('add_book.html',{'form':form}) 

還有就是我forms.py

class BookForm(ModelForm): 
    class Meta: 
     model = Book 
     exclude = ('publication_date',) 

而且我的模型

class Book(models.Model): 
    title = models.CharField(max_length = 100) 
    authors = models.ManyToManyField(Author) 
    publisher = models.ForeignKey(Publisher) 
    publication_date = models.DateField() 
    num_pages = models.IntegerField(blank = True, null = True) 

    class Admin: 
     list_display = ('title', 'publisher', 'publication_date') 
     list_filter = ('publisher', 'publication_date') 
     ordering = ('-publication_date',) 
     search_fields = ('title',) 
    def __str__(self): 
     return self.title 

我用這個模板的形式:

{% extends 'base.html' %} 
 
    {% block title %}Add a new Book{% endblock %} 
 
    {% block content %} 
 
    <h3> Here you can add a new book into the local DataBase </h3> 
 
    <form action="." method="post">{% csrf_token %}> 
 
     <div class="fieldWrapper"> 
 
     {{ form.title.errors }} 
 
     <label for="id_title">Book Title</label> 
 
     {{ form.title }} 
 
     </div> 
 
     <div class="fieldWrapper"> 
 
     {{ form.authors.errors }} 
 
     <label for="id_authors">Authores</label> 
 
     {{ form.authors }} 
 
     </div> 
 
     <div class="fieldWrapper"> 
 
     {{ form.publisher.errors }} 
 
     <label for="id_publisher">Publishers</label> 
 
     {{ form.publisher }} 
 
     </div> 
 
     <div class="fieldWrapper"> 
 
     {{ form.num_pages.errors }} 
 
     <label for="id_num_pages">Number of Pages</label> 
 
     {{ form.num_pages }} 
 
     </div> 
 

 

 
     <p><input type="submit" value="Submit"></p> 
 
    </form> 
 
    {% endblock %}

我暫時停用了Django的CSRF,因爲我不需要爲我的目的

回答

0

爲了做到這一點呢,你需要將default參數作爲timezone.nowpublication_date模型字段中傳遞。

models.py

class Book(models.Model): 
    title = models.CharField(max_length = 100) 
    authors = models.ManyToManyField(Author) 
    publisher = models.ForeignKey(Publisher) 

    # pass the default argument in publication_date field 
    publication_date = models.DateField(default=timezone.now) 

    num_pages = models.IntegerField(blank = True, null = True) 

    class Admin: 
     list_display = ('title', 'publisher', 'publication_date') 
     list_filter = ('publisher', 'publication_date') 
     ordering = ('-publication_date',) 
     search_fields = ('title',) 
    def __str__(self): 
     return self.title 

views.py

這樣做之後,你可以直接調用的ModelForm .save()。現在,圖書對象將使用知曉的日期時間創建。

def add_book(request): 
    if request.method == 'POST': 
     form = BookForm(request.POST) 
     if form.is_valid(): 
      new_book = form.save() # directly save the book object 
      return HttpResponseRedirect('/thanks/') 
     else: 
      print form.errors 
    else: 
     form = BookForm() 
    return render_to_response('add_book.html',{'form':form}) 

爲什麼我們沒有在模型領域使用auto_now_add的說法?

從Django的auto_now_add documentation:

Django的auto_now_add使用當前時間未時區察覺。因此,最好使用default參數明確指定默認值。

如果你希望能夠修改此字段中,設置的default=timezone.now (從django.utils.timezone.now()),而不是auto_now_add=True