how to get complaint details of all the users in django except the user logged in

I am creating a system where users can log in and enter complaints, view and edit them as well and also view complaints of other users on a different page. I have already created a page where they can view their own complaints but i don't know how to let them view other users complaints. Can someone please tell me what to type into the views.py and the template? Do i also need to make a new form for this?

The page is to look like this:

enter image description here

models.py:

from django.db import models
from django.contrib.auth.models import User
from django.db.models.deletion import CASCADE

class Profile(models.Model):
   user = models.OneToOneField(User,null= True , blank = True, on_delete= models.CASCADE)
   profile_pic = models.ImageField(default = "msi.jpg", null = True, blank= True, upload_to= 
  'static/profileimages')
   first = models.CharField(max_length=500, null=True)
   last = models.CharField(max_length=500, null=True)
   email = models.CharField(max_length=500, null=True)
   mobile_number = models.IntegerField(null=True)
   location = models.CharField(max_length= 500, null= True)
   postal = models.IntegerField(null=True)

   def __str__(self):
       return self.first


class Complaint(models.Model):
   user = models.ForeignKey(User, on_delete= models.CASCADE, null = True, blank=True)
   id = models.AutoField(blank=False, primary_key=True)
   reportnumber = models.CharField(max_length=500 ,null = True, blank= False)
   eventdate = models.DateField(null=True, blank=False)
   event_type = models.CharField(max_length=300, null=True, blank=True)
   device_problem = models.CharField(max_length=300, null=True, blank=True)
   manufacturer = models.CharField(max_length=300, null=True, blank=True)
   product_code = models.CharField(max_length=300, null=True, blank=True)
   brand_name = models.CharField(max_length = 300, null=True, blank=True)
   exemption = models.CharField(max_length=300, null=True, blank=True)
   patient_problem = models.CharField(max_length=500, null=True, blank=True)
   event_text = models.TextField(null=True, blank= True)
   document = models.FileField(upload_to='static/documents', blank=True, null=True)

   def __str__(self):
       return self.reportnumber

forms.py:

class DateInput(forms.DateInput):
input_type = 'date'

class ComplaintForm(ModelForm):
   class Meta:
       model = Complaint
       fields = '__all__'
       widgets = {
           'reportnumber': forms.TextInput(attrs={'placeholder': 'Report number'}),
           'event_type': forms.TextInput(attrs={'placeholder': 'Event type'}),
           'eventdate': DateInput(),
           'device_problem': forms.TextInput(attrs={'placeholder': 'Device Problem'}),
           'event_text': forms.Textarea(attrs={'style': 'height: 130px;width:760px'}),
           'manufacturer': forms.TextInput(attrs={'placeholder': 'Enter Manufacturer Name'}),
           'product_code': forms.TextInput(attrs={'placeholder': 'Enter Product Code'}),
           'brand_name': forms.TextInput(attrs={'placeholder': 'Enter Brand Name'}),
           'exemption': forms.TextInput(attrs={'placeholder': 'Enter Exemption'}),
           'patient_problem': forms.TextInput(attrs={'placeholder': 'Enter Patient Problem'}),
        }
    
   def clean(self):
       cleaned_data = super(ComplaintForm, self).clean()
       reportnumber = cleaned_data.get('reportnumber')
       event_text = cleaned_data.get('event_text')
       if not reportnumber and not event_text:
           raise forms.ValidationError('You have to write something!')
       return cleaned_data

class EditComplaintForm(ModelForm):
   class Meta:
       model = Complaint
       fields = ['reportnumber', 'event_type', 'eventdate', 'device_problem', 
       'device_problem', 'event_text', 'manufacturer', 'product_code', 'brand_name', 
       'exemption', 'patient_problem', 'document' ]
       widgets = {
           'reportnumber': forms.TextInput(attrs={'placeholder': 'Report number'}),
           'event_type': forms.TextInput(attrs={'placeholder': 'Event type'}),
           'eventdate': DateInput(),
           'device_problem': forms.TextInput(attrs={'placeholder': 'Device Problem'}),
           'event_text': forms.Textarea(attrs={'style': 'height: 130px;width:760px'}),
           'manufacturer': forms.TextInput(attrs={'placeholder': 'Enter Manufacturer Name'}),
           'product_code': forms.TextInput(attrs={'placeholder': 'Enter Product Code'}),
           'brand_name': forms.TextInput(attrs={'placeholder': 'Enter Brand Name'}),
           'exemption': forms.TextInput(attrs={'placeholder': 'Enter Exemption'}),
           'patient_problem': forms.TextInput(attrs={'placeholder': 'Enter Patient Problem'}),
       }   
    
   def clean(self):
       cleaned_data = super(EditComplaintForm, self).clean()
       reportnumber = cleaned_data.get('reportnumber')
       event_text = cleaned_data.get('event_text')
       if not reportnumber and not event_text:
           raise forms.ValidationError('You have to write something!')
       return cleaned_data

template:

<div class="col-lg middle middle-complaint-con">
        <i class="fas fa-folder-open fa-4x comp-folder-icon"></i>
        <h1 class="all-comp">All Complaints</h1>
        <p class="all-comp-txt">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>

        <a href="new.html" style="color:black;">
            <div class="container comp-con-0">
                <img src="{% static 'profileimages/msi.jpg' %}" class="comp-con-img" alt="">
                <p class="comp-level-1">High</p>
                <p class="comp-title-1">Complaint Title</p>
                <p class="comp-sub-1">Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
            </div>
        </a>

        <a href="new.html" style="color:black;">
            <div class="container comp-con-1">
                <img src="{% static 'profileimages/msi.jpg' %}" class="comp-con-img" alt="">
                <p class="comp-level-2">Low</p>
                <p class="comp-title-2">Complaint Title</p>
                <p class="comp-sub-2">Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
            </div>
        </a>

        <a href="new.html" style="color:black;">
            <div class="container comp-con-2">
                <img src="{% static 'profileimages/msi.jpg' %}" class="comp-con-img" alt="">
                <p class="comp-level-2">Low</p>
                <p class="comp-title-2">Complaint Title</p>
                <p class="comp-sub-2">Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
            </div>
        </a>

        <a href="new.html" style="color:black;">
            <div class="container comp-con-3">
                <img src="{% static 'profileimages/msi.jpg' %}" class="comp-con-img" alt="">
                <p class="comp-level-1">High</p>
                <p class="comp-title-1">Complaint Title</p>
                <p class="comp-sub-1">Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
            </div>
    </div>
    </a>

How can a user see all the other users complaints as well but not their own complaints on this page?



Read more here: https://stackoverflow.com/questions/68470379/how-to-get-complaint-details-of-all-the-users-in-django-except-the-user-logged-i

Content Attribution

This content was originally published by Isha Jagadi at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: