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",
)