3

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.

2
  • What's the full trace? Where is it telling you it has no attribute view_id? Commented Mar 22, 2017 at 18:58
  • Is telling me this in the django debug, I've also added the traceback now. Commented Mar 22, 2017 at 19:00

3 Answers 3

9

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()
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

0

@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'

Comments

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.