Django: Display data from other table

I’m having a hard time displaying some datas from two or three tables. So this is my code for showing all the data only from Device table. def device(request): if request.method == ‘GET’: queryset = Device.objects.all() if queryset: data = {"queryset": queryset} return render(request, ‘device/device.html’, data) else: return render(request, ‘device/device.html’) And I want to add… Read More Django: Display data from other table

Django model reference when using AbstractBaseUser model

I ‘m following a tutorial of how to use AbstractBaseUser model in Django project. Now I would like to go one step further by creating other models for example address_book and product. When using defaulter user model, we put like this: class User(models.Model): …. class AddressBook(models.Model): …. class Product(models.Model): …. Now when I use like… Read More Django model reference when using AbstractBaseUser model

Difference between request.user vs. get_user(request) in Django?

I noticed there are two ways to get a user object from request (assuming user is already logged in and the session is valid): user = request.user user = get_user(request) where get_user() is imported from django.contrib.auth. What’s the difference? get_user() seems to do a lot of validation for request session. Which is better? >Solution :… Read More Difference between request.user vs. get_user(request) in Django?

Display a product whose ID is passed as a parameter

I would like to retrieve the product whose ‘id’ is passed as a parameter, how do I do this? For example here I passed the id equal to 1 Note I don’t use a model but a dictionary def cart_add(request, id): dico={"produits":[{‘id’:1,’name’:’pomme de terre’,’price’:1250}]} mes_produits=dico[‘produits’] cart = Cart(request) mes_produits[‘id’]=id product=mes_produits[‘id’] cart.add(product=product) return render(request, ‘cart/cart_detail.html’) i… Read More Display a product whose ID is passed as a parameter

Django convert Decimal into integer when point value is 0

I am trying to convert a Django Decimal field into an integer but only if it has a 0 point value. So basically: decimal_field = models.DecimalField(max_digits=10, decimal_places=2, default=0) Lets say we have the following decimal_field = 15.6 Then when I use if int(decimal_field): decimal_field = int(decimal_field) It gets converted to 15 instead of staying 15.6… Read More Django convert Decimal into integer when point value is 0

Django ModuleNotFoundError after creating models

I’ve started learning Django and struggling with creating/migrating a model. Everytime I try to type python manage.py migrate(or anything), I get the error ModuleNotFoundError: No module named ‘People’. Here’s my directory structure; |- VLCase |- VLCase |- models.py |- urls.py |- settings.py |- asgi.py |- __pycache__ |- wsgi.py |- manage.py |- db.sqlite3 Here’s my models.py;… Read More Django ModuleNotFoundError after creating models

How to display the label from models.TextChoices in the template?

The Django docs says that one can use .label, but it does not work in the template. class Model(models.Model): class ModelChoices(models.TextChoices): ENUM = ‘VALUE’, ‘Label’ model_choice = models.CharField(choices=ModelChoices.choices) In the template object.model_choice displays the value (‘VALUE’). object.model_choice.label displays nothing. How is it possible to get the label (‘Label’) in the template? Thanks for your time… Read More How to display the label from models.TextChoices in the template?