diff --git a/source/_posts/django-custom-user-model.md b/source/_posts/django-custom-user-model.md new file mode 100644 index 00000000..96fa0427 --- /dev/null +++ b/source/_posts/django-custom-user-model.md @@ -0,0 +1,91 @@ +--- +title: Django Custom User Model +date: 2020-11-30 11:29:00 +author: Himanshu Garg +tags: + - user-model + - custom-user-model + - python + - django + - django-admin +categories: ['python'] + +--- +![Django Custom User Model](/blog/python/django-custom-user-model/custom-user-model-logo.png) + + +“How do I replace the **username** field with the **email** field or add other fields while for registering a new user? ” + +Did this question came to your mind before creating any application with django? If yes, then this post is for you. + +In this post, I will show you guys how to create custom user model in django. + +As we all know the default **User** model in django uses a username to uniquely identify a user during authentication. If you need to replace this username field with other field lets say an email field then you need to create a custom user model by either subclassing **AbstractUser** or **AbstractBaseUser**. + +So what are these ? + +**AbstractUser :-** if you only want to remove the username field and don’t have any problem with other fields + +**AbstractBaseUser :-** if you want to start all from scratch by creating your complete new user model + +## **Code** + +I am assuming you already have created a django-project. So create a new app *users.* + + python manage.py startapp users + +Lets create a users/managers.py file + +![](https://cdn-images-1.medium.com/max/2616/1*7PgkURTjGBX6DFrdEK7G-A.png) + +Now create a users/models.py file + +![](https://cdn-images-1.medium.com/max/2580/1*nkmzZxdSvBUGHIJ3eSGYLA.png) + +Here we firstly created our custom manager by subclassing **BaseUserManager**, that uses email for unique identifies rather than username + +Also for the models.py file, we firstly subclass **AbstractUser**. Here, we specify email as **USERNAME_FIELD**. Along wih these we also added a new field to store the **Full Name** of the user. + +Lets create the users/forms.py file + +![](https://cdn-images-1.medium.com/max/2984/1*B-cTrFmAhIGBichsHYvC9w.png) + +Here in forms.py we have created a basic validation method so that while creating the user, both the passwords are same + +users/views.py + +![](https://cdn-images-1.medium.com/max/2548/1*F0HjR9GvkLuWEUM04qE1EA.png) + +It imports our custom user model and saves the fields to the model + +Before running the code, lets change our project settings.py file + +![](https://cdn-images-1.medium.com/max/2000/1*BhVktNIey9hvH0Mj56Jvsw.png) + +Here, in settings.py file we firstly added our newly created app. Then we define a **AUTH_USER_MODEL** as we have created our custom model. + +Lets migrate our model + + python manage.py makemigration + python manage.py migrate + python manage.py runserver + +You can check the urls and other template files from my github repo + +**Output** + +![sample form values](https://cdn-images-1.medium.com/max/3188/1*HXD8q2zpp7RX9BdLJyGUPg.png) + +In django-admin + +![Some users which I have created](https://cdn-images-1.medium.com/max/2000/1*18_3VxHJn1ryipLC6LMHsA.png) + +![](https://cdn-images-1.medium.com/max/2000/1*CcoigQy7gvG-qiiUnO00Yg.png) + +## **Conclusion** + +In this post, we looked at how to create a custom django user model. You can find the final code [here](https://github.com/hghimanshu/Blog/tree/master/custom). + +**Some good reads you may like :)** + +1. [Django vs Flask which framework to choose](https://nayan.co/blog/python/django-vs-flask-which-framework-to-choose/) \ No newline at end of file diff --git a/source/_posts/django-custom-user-model/custom-user-model-logo.png b/source/_posts/django-custom-user-model/custom-user-model-logo.png new file mode 100644 index 00000000..7f6f1a3f Binary files /dev/null and b/source/_posts/django-custom-user-model/custom-user-model-logo.png differ diff --git a/source/_posts/reducing-the-docker-image-sizes.md b/source/_posts/reducing-the-docker-image-sizes.md new file mode 100644 index 00000000..b6979546 --- /dev/null +++ b/source/_posts/reducing-the-docker-image-sizes.md @@ -0,0 +1,64 @@ +--- +title: Reducing the docker image sizes +date: 2020-11-30 11:29:00 +author: Himanshu Garg +tags: + - docker + - lightweight images + - python + - docker-images + - alpine +categories: ['python'] + +--- +![](/blog/python/reducing-the-docker-image-sizes/docker.png) + +While building any docker image, do we get an idea about the size of that image and can we optimize their size? + +Well in this post I will tell you how can you optimize the size of your very heavy docker images. + +## **Lets gets started!!** + +Before beginning I’ll recommend you to go through my previous blog in which I had show you how to create docker images. In this post I’ll use that docker image and try to optimize it as much I can. + +![](https://cdn-images-1.medium.com/max/2000/1*5mnlDgrO84nUwzGDKMgQrw.png) + +Here, the image shown as **docker_containerized_cifar** was the one which we created in the post. Also, as the image shows you, this images is of **3.79GB**. That’s very heavy image. Lets us try to optimize and see how much can we reduce in this image + +Before going anything further, lets see sizes of layers in this image + + docker history docker_containerized_cifar + +![](https://cdn-images-1.medium.com/max/2178/1*ey_DuT_XlfHGUQtgTFQAdQ.png) + +Here, the size of each layer is shown. Lets try to improve this + +I have create another dockerfile. Here I have reduce the RUN command as every RUN adds a new layer hence increase the size of the image. + +So, I have build a new image + +![](https://cdn-images-1.medium.com/max/2000/1*rGzaepne58vjXrddBmM0Nw.png) + +Lets check its size and compare it with the previous image + +![](https://cdn-images-1.medium.com/max/2000/1*wiPQ4V707I7A7Lo9NOLqkw.png) + +Here, as you can see the new image **lightcifar_1** is of around **3GB** which is quite less than the original image. So, we have reduced around **0.79GB** of size and that too in one go 🙌. Lets try one more time and try to reduce this by using a small base image and see. + +![](https://cdn-images-1.medium.com/max/2000/1*1LFPYX_HBmVAC0QyUy6ZEA.png) + +Lets check its size and compare it with the previous images + +![](https://cdn-images-1.medium.com/max/2000/1*NAmm6pqnIM2_35R5DrmtlQ.png) + +This new image name is **lightcifar_2**. Here we have take lighter base image and wow our image size is reduced to **2.48GB** from **3.79GB**. That’s around 34% lighter from the original image. Lets see its size of the layers + +![](https://cdn-images-1.medium.com/max/2298/1*ZHNBLTV9UCcJhWap4IzAvA.png) + +## **Conclusion** + +So, with this post we learn how to reduce the size of the heavy docker images. If you need any code helps then you can find the code from [here](https://github.com/hghimanshu/Blog/tree/master/CIFARDocker). If you also have some other way to reduce this size then please let me know. + +**Some good reads you may like :)** + +1. [Creating a very basic deep-learning model in Docker](https://nayan.co/blog//AI/Creating-deep-learning-models-in-Docker/) \ No newline at end of file diff --git a/source/_posts/reducing-the-docker-image-sizes/docker.png b/source/_posts/reducing-the-docker-image-sizes/docker.png new file mode 100644 index 00000000..f9d66def Binary files /dev/null and b/source/_posts/reducing-the-docker-image-sizes/docker.png differ