Isha Jagadi
Isha Jagadi

Reputation: 95

attribute error in django. django.db.models has no attribute

enter image description here

I am trying to write the code so that a user in the system can view complaint registered by others on this page but not their own complaints. This is the code, but I don't understand what's wrong:

views.py:

class OtherPeoplesComplaints(TemplateView):
   model = Complaint
   form_class = ComplaintForm
   template_name = 'userComplaints.html'
   def get_context_data(self, *args, **kwargs):
       context = super().get_context_data(*args, **kwargs)
       context["complaints"] = models.Complaint.objects.exclude(
           profile__user = self.request.user
       )

models.py:

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

Upvotes: 0

Views: 80

Answers (1)

Prakhar
Prakhar

Reputation: 1014

In this view

class OtherPeoplesComplaints(TemplateView):
   model = Complaint
   form_class = ComplaintForm
   template_name = 'userComplaints.html'
   def get_context_data(self, *args, **kwargs):
       context = super().get_context_data(*args, **kwargs)
       context["complaints"] = models.Complaint.objects.exclude(
           profile__user = self.request.user
       )

you are using this query :

context["complaints"] = models.Complaint.objects.exclude(profile__user = self.request.user)

change it to :

context["complaints"] = self.model.objects.exclude(user = self.request.user)

Explanation :

Your model is Complaint which you can access using self.model as you have defined the class variable here :

class OtherPeoplesComplaints(TemplateView):
   model = Complaint

and

You want to access complaint of other users except your own, so this update in your query :

exclude(user = self.request.user)

Upvotes: 1

Related Questions