You had the correct approach commented out, why?
https://docs.djangoproject.com/en/1.11/ref/models/querysets/#get-or-create get_or_create returns a tuple of the form (object, created [boolean]).
What you'd want to do is split out the result of that command:
objx, created = GoogleProperty.objects.get_or_create(google_email=current_user)
Then you can do:
if not created:
objx.view_id = view_id
objx.save()
Answer from mechanical_meat on Stack OverflowYou had the correct approach commented out, why?
https://docs.djangoproject.com/en/1.11/ref/models/querysets/#get-or-create get_or_create returns a tuple of the form (object, created [boolean]).
What you'd want to do is split out the result of that command:
objx, created = GoogleProperty.objects.get_or_create(google_email=current_user)
Then you can do:
if not created:
objx.view_id = view_id
objx.save()
You seem to have syntax errors. objx is a tuple, tuples are indexed by integers, and also tuples are immutable in python, so this should work.
objx[0].view_id = view_id
objx[0].save()
and defaults are provided to set default attributes incase of object creation. So basically you are setting a default view_id, if the object is created.
Provide the stack trace for the commented out part also.
django get_or_create return error: 'tuple' object has no attribute - Stack Overflow
Django AttributeError: 'tuple' object has no attribute 'get'
python - AttributeError 'tuple' object has no attribute 'get' - Stack Overflow
python - AttributeError: 'tuple' object has no attribute '_meta' error in Django - Stack Overflow
Videos
get_or_create does not just return the object:
Returns a tuple of
(object, created), where object is the retrieved or created object and created is a boolean specifying whether a new object was created.
In your case d has been assigned this tuple instead of the object you expected, so you get the attribute error. You can fix your code by changing it to:
d, created = DiaSemana.objects.get_or_create(dias=diaSemana)
The following two lines look unnecessary to me. The get_or_create call above ensures that d.dias=diaSemana, so there's no need to assign it again. There's probably no need to call save either.
d.dias = diaSemana;
d.save()
instead of this:
dias = models.CharField(max_length=20, choices=DIAS_CHOICES)
do:
dias = models.CharField(max_length=20, choices=DIAS_CHOICES)[0]
as @Alasdair said, the first one in the tuple is the object
from django.shortcuts import render
from django.http import HttpResponse
import datetime
def home(request):
now=datetime.datetime.now()
context={'datetime_now':now}
return render(request, 'home.html', context)You are returning a tuple here:
elif retailer_pk:
return (request, 'page-retailer-single.html', {
"products": products,
"sorting": filt["sorting"],
"filtering": filt["filtering"],
"retailer": retailer,
})
Did you forget to add render there perhaps:
elif retailer_pk:
return render(request, 'page-retailer-single.html', {
"products": products,
"sorting": filt["sorting"],
"filtering": filt["filtering"],
"retailer": retailer,
})
There is an extra comma "," at the end of return function in views.py
return render(request, 'index.html',{}), #incorrect
return render(request, 'index.html',{}) #correct
I am creating a Contact page for my django project. forms.py has name, subject, sender and message. Here's the view:
And the form:
<form method="POST">
{%csrf_token%}
{{form.as_p}}
<button type="submit">Send</button>
</form>
After I click Send I get this error: AttributeError: 'tuple' object has no attribute 'encode' and I can't seem to figure out what's wrong despite checking other questions of the same type.
I am using google SMTP server, from this link following its instructions.
You should provide the full trace back. We aren't mind readers.
Only thing I can think of is if you have other elements on the HTML form with the same name (”name”, “sender_email”, “subject”, or “message”). That, or try to remove the comma in the recipient list. I don’t think it’s the list, though.
The only google entry with the same error code was in relation to extending the User model, which I'm not doing. This error occurs after I submit the PaperEstimate form which of course uses django-braces. Is there a _default_manager in Braces that could be throwing an error based on the way I'm implementing Braces?
I'm having a hard time understanding what is going on when I get a traceback that doesn't include my own code. Could this be related to django-braces? Or maybe from my using generic class based views?
The Traceback: http://pastebin.com/Z4M26UR5 Relevant Code: http://pastebin.com/qY5Wpdjw
You have a typo - it should be fields, not field:
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = ('id', 'facebook_id', 'first_name', 'last_name', 'access_token')
# HERE ^
from rest_framework import serializers
from .models import Post
class PostSerializer(serializers.ModelSerializer):
class Meta:
model = Post
fields = ('title','slug','content')
still tuple has no attributes is showing
I am trying to make a discord bot that would turn a message into lowercase. I am encountering an error, as the title suggests, "AttributeError: 'tuple' object has no attribute 'lower'. "
Here is my code if anyone can help.
https://hastebin.com/ibareyilax.py
If this is the wrong subreddit I don't mind taking down my post and posting it elsewhere.