0

I have am trying to create a "stats" app in my project that keeps track of all the leads my site generates. When a user submit the "request info" form a message is automatically sent to the business associated with that product. Simultaneously I would like a model instance to be created in one of the models in the Stats app (different app then we are working in). The Stats works in the background simply collecting info view model instance for certain things. Here is the code breakdown:

The view:

def ListingView(request,name_initials,l_slug):
    listing = get_object_or_404(Listing,l_slug=l_slug)
    images = ListingImage.objects.filter(property=listing)
    form = ContactPropertyForm(request.POST or None)

    context = {
            'listing':listing,
            'images':images,
            'form':form,
            }

    if form.is_valid():
        name = form.cleaned_data.get('name')
        phone = form.cleaned_data.get('phone')
        email = form.cleaned_data.get('email')
        party_size = form.cleaned_data.get('party_size')
        form_message = form.cleaned_data.get('message')
        listing_address = listing.address
        message = name + "  " + phone + "   " + email + "   " + party_size + "  " + listing_address
        to_email = ['email here']
        html_message = "<b>Name: </b>" + name + "<br>" + "<b>Phone: </b>" + phone + "<br>" + "<b>Email: </b>" + email + "<br>" + "<b>Group Size: </b>" + party_size + "<br>" + "<b>Property: </b>" + listing_address
        send_mail('New Lead', message, 'from email', ['To email'], fail_silently=False, html_message=html_message)

        Leads.add(lead)
        lead = Leads.objects.create(
            listing = listing_address,
            company = listing.l_company,
            )


    return render(request,'listings/single_listing.html',context)

Specifically, this is the bit we are working with:

Leads.add(lead)
    lead = Leads.objects.create(
        listing = listing_address,
        company = listing.l_company,
        )

I am also getting this error upon form submission:

AttributeError at /tu/diamond-green-apartments
type object 'Leads' has no attribute 'add'

Maybe I'm not going about adding model instances correctly in separate app models?

Side note: For context, I adapted it from another project I had previously worked on, it was a part of a notification system. Here is what it originally looked like:

if user.is_authenticated():
    if user in obj.likes.all():
        obj.likes.remove(user)
    else:
        obj.likes.add(user)
        notification = UserNotification.objects.create(
            fromUser = self.request.user,
            toUser = obj.author,
            post = obj,
            notify_type = "like",
        )

1 Answer 1

1

Django add method using only to add instance to many-to-one or many-to-many related objects set. In you case Leads.add(lead) is unnecessary and you can remove it without any problems this line will add new Lead object itself:

lead = Leads.objects.create(
    listing = listing_address,
    company = listing.l_company,
)
Sign up to request clarification or add additional context in comments.

3 Comments

Gives an error: ValueError at /tu/diamond-green-apartments Cannot assign "'1000 Diamond St, Philadelphia, PA 19122, USA'": "Leads.listing" must be a "Listing" instance.
@Garrett oh, change to: lead = Leads.objects.create( listing = listing, company = listing.l_company, )
This ended up working but I realized I didn't want a ForeignKey but rather CharField for the name of the company and address. Doesn't need to be linked for anything. Thanks for the help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.