入门笔记翻译整理自:https://docs.djangoproject.com/en/1.8/topics/
*该笔记将对各个模块进行单独介绍
*migration&Managing files&Testing in Django
1. migration的命令
migrate命令负责应用migrations,同时也负责撤销migrations以及查看他们的状态。
makemigrations基于对模型的改变,创建新的migrations。
sqlmigrate为migration显示SQL语句。
2. 模型中的文件
1 from django.db import models 2 3 class Car(models.Model): 4 name = models.CharField(max_length=255) 5 price = models.DecimalField(max_digits=5, decimal_places=2) 6 photo = models.ImageField(upload_to=‘cars‘)
以上模型中,photo是文件。
1 >>> car = Car.objects.get(name="57 Chevy") 2 >>> car.photo 3 <ImageFieldFile: chevy.jpg> 4 >>> car.photo.name 5 ‘cars/chevy.jpg‘ 6 >>> car.photo.path 7 ‘/media/cars/chevy.jpg‘ 8 >>> car.photo.url 9 ‘http://media.example.com/cars/chevy.jpg
3. Django中的测试
1 from django.test import TestCase 2 from myapp.models import Animal 3 4 class AnimalTestCase(TestCase): 5 def setUp(self): 6 Animal.objects.create(name="lion", sound="roar") 7 Animal.objects.create(name="cat", sound="meow") 8 9 def test_animals_can_speak(self): 10 """Animals that can speak are correctly identified""" 11 lion = Animal.objects.get(name="lion") 12 cat = Animal.objects.get(name="cat") 13 self.assertEqual(lion.speak(), ‘The lion says "roar"‘) 14 self.assertEqual(cat.speak(), ‘The cat says "meow"‘)
-- The End --
时间: 2024-12-05 20:20:07