Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Object has no attribute issue

I’ve followed a tutorial on how to create a CRM via Python, Django and came across an issue in the end result.

When I attempt to convert a lead into a client, I am faced with ‘Lead’ object has no attribute ‘comment’. This is present if the lead has comments under it and if it does not.

But once I get back to the lead list, I notice that the lead has been converted and duplicate 4 times. Has anyone else come across this issue? And how would I go about fixing it?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Below is the models.py file

class Lead(models.Model):
    LOW = "low"
    MEDIUM = "medium"
    HIGH = "high"

    CHOICES_PRIORITY = ((LOW, "Low"), (MEDIUM, "Medium"), (HIGH, "High"))

    team = models.ForeignKey(Team, related_name="leads", on_delete=models.CASCADE)
    name = models.CharField(max_length=255)
    email = models.EmailField()
    number = models.CharField(max_length=255)
    description = models.TextField(blank=True, null=True)
    priority = models.CharField(max_length=10, choices=CHOICES_PRIORITY, default=MEDIUM)
    converted_to_client = models.BooleanField(default=False)
    created_by = models.ForeignKey(
        User, related_name="leads", on_delete=models.CASCADE, default="Admin"
    )
    created_at = models.DateTimeField(auto_now_add=True)
    modified_at = models.DateTimeField(auto_now=True)

    class Meta:
        ordering = ("name",)

    def __str__(self):
        return self.name

class Comment(models.Model):
    team = models.ForeignKey(
        Team, related_name="lead_comments", on_delete=models.CASCADE
    )
    lead = models.ForeignKey(Lead, related_name="comments", on_delete=models.CASCADE)
    content = models.TextField(blank=True, null=True)
    created_by = models.ForeignKey(
        User, related_name="lead_comments", on_delete=models.CASCADE
    )
    created_at = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.created_by.username

This is the error shown:

‘Lead’ object has no attribute ‘comment’
Request Method: GET
Request URL: http://127.0.0.1:8000/dashboard/leads/17/convert/
Django Version: 4.2.1
Exception Type: AttributeError
Exception Value:
‘Lead’ object has no attribute ‘comment’

Below is the views.py file:

class ConvertToClientView(LoginRequiredMixin, View):
    def get(self, request, *args, **kwargs):
        pk = self.kwargs.get("pk")

        lead = get_object_or_404(Lead, created_by=request.user, pk=pk)
        team = self.request.user.userprofile.active_team

        client = Client.objects.create(
            name=lead.name,
            email=lead.email,
            number=lead.number,
            description=lead.description,
            created_by=request.user,
            team=team,
        )

        lead.converted_to_client = True
        lead.save()

        # Convert Comments from Lead to Client Comments

        comments = lead.comment.all()

        for comment in comments:
            ClientComment.objects.create(
                client=client,
                content=comment.content,
                created_by=comment.created_by,
                team=team,
            )

        messages.success(request, "The lead was converted")

        return redirect("leads:list")

I’ve attempted to place an if statement prior to the for loop, however it did not resolve the matter.

I’m hoping to get rid of the attribute error, and prevent the action from duplicating the objects.

>Solution :

Your related_name of Comment to Lead relation is comments

lead = models.ForeignKey(Lead, related_name="comments", on_delete=models.CASCADE)

but you refer it as comment

comments = lead.comment.all()

So it’s just a typo

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading