Home > Software design >  How to display only the appointments that the current logged in user has made instead of fetching up
How to display only the appointments that the current logged in user has made instead of fetching up

Time:12-09

This the views.py file. How can i display the appointments made by the current logged in user?

def user(request):
    client = Client.objects.all()
    appointments = Appointment.objects.all()

    context = {'appointments': appointments, 'client': client,
               }

    return render(request, 'users/user.html', context)

Here is my Models.py. I need to display the appointments by a user when they are logged in to their profile.

class Appointment(models.Model):
    CATEGORY = (
        ('Plumbing', 'Plumbing'),
        ('Electrical', 'Electrical'),
        ('Cleaning', 'Cleaning'),
    )
    STATUS = (
        ('Pending', 'Pending'),
        ('Delivered', 'Delivered'),
    )

    user = models.ForeignKey(Client, null=True, on_delete=models.SET_NULL)
    name = models.CharField(max_length=200, null=True)
    worker = models.ForeignKey(Worker, null=True, on_delete=models.SET_NULL)
    category = models.CharField(max_length=200, null=True, choices=CATEGORY)
    task_date = models.DateField(_("Task Date"), blank=True, null=True)
    task_location = models.CharField(max_length=200, null=True)
    date_created = models.DateTimeField(auto_now_add=True, null=True)
    status = models.CharField(max_length=200, null=True, choices=STATUS)
    budget = models.FloatField(null=True)
    task_description = models.CharField(max_length=1000, null=True)
    task_image = models.ImageField(
        null=True, blank=True, help_text='Optional.')

    def __str__(self):
        return str(self.user)

CodePudding user response:

instead of using all() in your query use filter() all() gives you all the entries in the table. do something like this:

appointments = Appointment.objects.filter(user = request.user)

the left side "user" inside the filter must be a column in the Appointment model/table. you can pass multiple parameters inside the filter.

  • Related