DJango displaying incorrect view

I am trying my best to learn Django for the past few days, and I came across an unusual problem. I know this is very basic form some, so I’m asking for your help on this one. I created two different views that will show two different outputs. from django.shortcuts import render from django.template import… Read More DJango displaying incorrect view

how to set query for show followed posts in home page

I want to make query to show all followed posts in the main page, could you help me in doing this? Here’s my file models.py: class Relation(models.Model): from_user = models.ForeignKey(User, on_delete=models.CASCADE, related_name=’follower’) to_user = models.ForeignKey(User, on_delete=models.CASCADE, related_name=’following’) created = models.DateTimeField(auto_now_add=True) class Profile(models.Model): user = models.OneToOneField(User, on_delete=models.CASCADE, related_name=’profile’) avatar = models.FileField(default=’default.jpg’, verbose_name=’avatar’) age = models.PositiveSmallIntegerField(default=0) location… Read More how to set query for show followed posts in home page

How to fetch a boolean value from models for a specific user in Django?

I have an extended user model and I want to check it to see if the logged-in user has completed_application as per the model: Models.py: class Completed(models.Model): class Meta: verbose_name_plural = ‘Completed Application’ user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE) completed_application = models.BooleanField(default=False) def __str__(self): return f'{self.completed_application}’ Views.py: @login_required def dashboard(request): if Completed.objects.get(completed_application = True): completed = True… Read More How to fetch a boolean value from models for a specific user in Django?

Django: Filtering items in generic.ListView

I’m creating a game website and i have these models for games: class Game(models.Model): accountant = models.ForeignKey(get_user_model(), on_delete=models.CASCADE, related_name=’games’) title = models.CharField(max_length=50) bank_money = models.IntegerField() player_starting_money = models.IntegerField() golden_card_amount = models.PositiveIntegerField( validators=[ MinValueValidator(100), MaxValueValidator(1000) ] ) now i want every user to see their own games at dashbard: class DashboardView(mixins.LoginRequiredMixin, generic.ListView): template_name = ‘games/dashboard.html’ model… Read More Django: Filtering items in generic.ListView

Django error ValueError: Field 'id' expected a number but got ''

I create django model for keep api_key as char like this. class DeviceKey(models.Model): api_key=models.CharField(max_length=100,unique=True) limit_device=models.IntegerField() created=models.DateTimeField(auto_now_add=True) updated=models.DateTimeField(auto_now=True) def __str__(self): return self.api_key class Meta: ordering=(‘api_key’,) verbose_name=’DEVICE KEY’ verbose_name_plural=’DEVICE KEY’ class Device(models.Model): api_key=models.ForeignKey(DeviceKey,on_delete=models.CASCADE) name = models.CharField(max_length=100) status=models.BooleanField() switch=models.BooleanField(default=False) timer=models.BooleanField(default=False) category=models.ForeignKey(Category,on_delete=models.CASCADE) created=models.DateTimeField(auto_now_add=True) updated=models.DateTimeField(auto_now=True) def __str__(self): return self.name class Meta: ordering=(‘api_key’,) I create function device() in views.py for get… Read More Django error ValueError: Field 'id' expected a number but got ''

How to obtain a specific data from model in profile when person is logged in, Django?

I want to post a specific data in profile from user that is logged in. I have a model like this: class Customer(models.Model): user = models.OneToOneField(User, null=True, on_delete=models.CASCADE) name = models.CharField(max_length=60) email = models.EmailField(max_length=100) class Wallet(models.Model): name = models.OneToOneField(User, null=True, on_delete=models.CASCADE) usd = models.FloatField(null=True) If for example ‘user2’ is logged in, and in database he… Read More How to obtain a specific data from model in profile when person is logged in, Django?

Django raise ValueError( ValueError: Cannot assign "1": "Order.variation" must be a "ProductVariation" instance

Im trying to create a Orderobject but I get this error, and I dont know how to fix variation_obj = ProductVariation.objects.get(id=int(variation_id_list[index])) quantity = variation_quantity_list[index] total = variation_total_list[index] total = float(total) order_object = Order(user=request.user, variation=variation_obj.id, quantity=quantity, total=total) error: Django raise ValueError( ValueError: Cannot assign "1": "Order.variation" must be a "ProductVariation" instance. models.py class Order(models.Model): variation =… Read More Django raise ValueError( ValueError: Cannot assign "1": "Order.variation" must be a "ProductVariation" instance

Django: how to get the user from user form input

I am trying to implement an attendance system as part of a bigger project that handles multiple schools at the same time, I am trying to get some user details from the user that is being marked as present, absent, or on leave serializer.py class AttendanceSerializer(serializers.ModelSerializer): class Meta: model = Attendance fields = [‘user’, ‘Presence’,… Read More Django: how to get the user from user form input

How to route URL pattern with URL path separator?

I have to route the following URL pattern to view api/v1/{resourceId}/owners in DRF But the problem is resourceID contains / in it. eg api/v1/somethings/value/owners the additional / causing get 404 resource not found exception Is there way to route the URL and get resourceID in view My urls.py path(‘api/v1/<str:resource_id>/owners’, ResourceOwnershipView.as_view()) views.py class ResourceOwnershipView(APIView): def get(self,… Read More How to route URL pattern with URL path separator?