Costantin
Costantin

Reputation: 2656

'tuple' object has no attribute - Django

I'm trying to save some data to my database, but I keep getting the error: 'tuple' object has no attribute 'view_id I think that somehow I'm getting the object wrongly from my db.

Models.py

class GoogleProperty(models.Model): # Settings for each single site, a user can have many!
    user = models.CharField(max_length=200) # Owner of site
    google_email = models.EmailField(max_length=200)
    profile_id = models.CharField(max_length=200) # Needed to delete users, but should be removed!

    property_name = models.CharField(max_length=200, blank=True, null=True)
    property_id = models.CharField(max_length=200, blank=True, null=True)
    property_url = models.CharField(max_length=200, blank=True, null=True)

    view_id = models.CharField(max_length=200)

    def __str__(self):
        return str(self.property_url)

Views.py

def some_function(requests):

    if len(list_of_views)==1: # Save to DB and call it a day
      view_name = list_of_views[0]['name']
      view_id = list_of_views[0]['id']
      print(view_id) # 123823290
      dic_views[view_name]=view_id

      # Save View to DB
      '''
      obj, created = GoogleProperty.objects.get_or_create(google_email=current_user, defaults={'view_id': view_id})
      if not created:
        obj.view_id = view_id
        obj.save()
      '''
      objx = GoogleProperty.objects.get_or_create(google_email=current_user)
      print(objx)    # (<GoogleProperty: None>, False)
      objx.view_id = view_id
      objx.save
      return HttpResponse(request, '/ga_app/report.html', {'view_id':view_id})

    else:  # Many Views, ask which one they want to track
        Do something

edit added traceback:

  File "/my-path/views.py", line 212, in select_view
objx.view_id = view_id
 AttributeError: 'tuple' object has no attribute 'view_id'

I've put as side note the results of the print() function. Also, in some parts of my code I add the defaults={'view_id': view_id} is that really needed? If so why?

P.s. I tried both codes that commented out and the one not commented out.

Upvotes: 3

Views: 9301

Answers (3)

Reidel
Reidel

Reputation: 377

@Costantin : You are calling to a property of tuple object that no exist so that is why you have getting error ex:

>>> t = ('23','e')
>>> t[1]
'e'
>>> t.r
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
AttributeError: 'tuple' object has no attribute 'r'

Upvotes: 0

Gautham Kumaran
Gautham Kumaran

Reputation: 441

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.

Upvotes: 0

mechanical_meat
mechanical_meat

Reputation: 169364

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()

Upvotes: 9

Related Questions