1. import datetime
    
  2. 
    
  3. from django import forms
    
  4. from django.test import TestCase
    
  5. 
    
  6. from .models import Article
    
  7. 
    
  8. 
    
  9. class FormsTests(TestCase):
    
  10.     # ForeignObjects should not have any form fields, currently the user needs
    
  11.     # to manually deal with the foreignobject relation.
    
  12.     class ArticleForm(forms.ModelForm):
    
  13.         class Meta:
    
  14.             model = Article
    
  15.             fields = "__all__"
    
  16. 
    
  17.     def test_foreign_object_form(self):
    
  18.         # A very crude test checking that the non-concrete fields do not get
    
  19.         # form fields.
    
  20.         form = FormsTests.ArticleForm()
    
  21.         self.assertIn("id_pub_date", form.as_table())
    
  22.         self.assertNotIn("active_translation", form.as_table())
    
  23.         form = FormsTests.ArticleForm(data={"pub_date": str(datetime.date.today())})
    
  24.         self.assertTrue(form.is_valid())
    
  25.         a = form.save()
    
  26.         self.assertEqual(a.pub_date, datetime.date.today())
    
  27.         form = FormsTests.ArticleForm(instance=a, data={"pub_date": "2013-01-01"})
    
  28.         a2 = form.save()
    
  29.         self.assertEqual(a.pk, a2.pk)
    
  30.         self.assertEqual(a2.pub_date, datetime.date(2013, 1, 1))