[Mobilar] 07 - REST API for Photo Mgt
多源组分发
Ref: [AWS] Web Server with Cloudfront
-
基本版本 without Cloudfront.
Ref: Upload images | DJANGO REST FRAMEWORK and AMAZON S3
Django with React | An Ecommerce Website
Build an eCommerce platform from the ground up with React, Redux, Django & Postgres
Course
Pay: Build a Backend REST API with Python & Django - Advanced
Create an advanced REST API with Python, Django REST Framework and Docker using Test Driven Development (TDD)
User Model
Django default user model is not good. So create a custom model for new projects.
-
自定义 User Model
class User(AbstractBaseUser, PermissionsMixin): """User in the system.""" email = models.EmailField(max_length=255, unique=True) name = models.CharField(max_length=255) is_active = models.BooleanField(default=True) is_staff = models.BooleanField(default=False) objects = UserManager() # ----> USERNAME_FIELD = 'email'
class UserManager(BaseUserManager): """Manager for users.""" def create_user(self, email, password=None, **extra_fields): """Create, save and return a new user.""" if not email: raise ValueError('User must have an email address.') user = self.model(email=self.normalize_email(email), **extra_fields) user.set_password(password) user.save(using=self._db) return user def create_superuser(self, email, password): """Create and return a new superuser.""" user = self.create_user(email, password) user.is_staff = True # --> more user.is_superuser = True # --> more user.save(using=self._db) return user
添加超级用户,可见,这里改为了 login by email, but not username as default.
$ docker-compose run --rm app sh -c "python manage.py createsuperuser" Starting c2-app-api-2_db_1 ... done Email: hao123 Password: Password (again): Superuser created successfully.
settings.py
AUTH_USER_MODEL = 'core.User'
-
定制 Django Admin
在此之前,可以先从 test 入手。
class AdminSiteTests(TestCase): """Tests for Django admin.""" def setUp(self): """Create user and client.""" self.client = Client() self.admin_user = get_user_model().objects.create_superuser( email='admin@example.com', password='testpass123', ) self.client.force_login(self.admin_user) self.user = get_user_model().objects.create_user( email='user@example.com', password='testpass123', name='Test User' ) def test_users_lists(self): """Test that users are listed on page.""" url = reverse('admin:core_user_changelist') res = self.client.get(url) self.assertContains(res, self.user.name) self.assertContains(res, self.user.email) def test_edit_user_page(self): """Test the edit user page works.""" url = reverse('admin:core_user_change', args=[self.user.id]) res = self.client.get(url) self.assertEqual(res.status_code, 200) def test_create_user_page(self): """Test the create user page works.""" url = reverse('admin:core_user_add') res = self.client.get(url) self.assertEqual(res.status_code, 200)
这里是 admin.py。
class UserAdmin(BaseUserAdmin): """Define the admin pages for users.""" ordering = ['id'] list_display = ['email', 'name'] fieldsets = ( (None, {'fields': ('email', 'password')}), (
_('Personal Info'),
{
'fields': (
'name',
)
}
), ( _('Permissions'), { 'fields': ( 'is_active', 'is_staff', 'is_superuser', ) } ), (_('Important dates'), {'fields': ('last_login',)}), # <---- default item ) readonly_fields = ['last_login'] add_fieldsets = ( (None, { 'classes': ('wide',), # --> css 'fields': ( 'email', 'password1', 'password2', 'name', 'is_active', 'is_staff', 'is_superuser', ), }), )
According to Django Documentation : "Two useful classes defined by the default admin site stylesheet are collapse and wide. Fieldsets with the collapse style will be initially collapsed in the admin and replaced with a small “click to expand” link. Fieldsets with the wide style will be given extra horizontal space." Try to replace extrapetty by collapse on your example and see what's happen.
转移到:[Django] 18 - DRF: Django Rest Framework