Home Django .get() returns a tuple and not the object
Reply: 1

Django .get() returns a tuple and not the object

Costantin
1#
Costantin Published in 2017-09-14 00:08:31Z

I have a simple function which looks like this:

parent_key = SeoKeys.objects.get(view_id=view_id, key_nbr=key_nbr)
    if parent_key.status != 'active':
        parent_key.status = status
        parent_key.save()

    metrics, created = SeoMetrics.objects.get_or_create(
                                                        seo_url = url_sent,
                                                        date = date,
                                                        parent_key = parent_key,
                                                        defaults = {
                                                                'parent_key':parent_key,
                                                                'seo_url': url_sent,
                                                                'url_found':url_found,
                                                                'position':position,
                                                                }
                                                        )

Now in theory this should work, however I get the following error:

ValueError: Cannot assign "(<SeoKeys: SeoKeys object>,)": "SeoMetrics.parent_key" must be a "SeoKeys" instance.

This happens because it's a tuple. If I do 'parent_key':parent_key[0] it will save it fine. However this seems a rather hacked solution and I would like to rather understand why this happens. Any ideas?

My model looks something like this:

class SeoMetrics(models.Model):
    parent_key = models.ForeignKey('SeoKeys', on_delete=models.CASCADE)

Edit: Added the full error:

Internal Server Error: /hook/
Traceback (most recent call last):
  File "/Users/Costantin/GDrive/Analytic.me/dev/venv/lib/python3.5/site-packages/django/core/handlers/exception.py", line 41, in inner
    response = get_response(request)
  File "/Users/Costantin/GDrive/Analytic.me/dev/venv/lib/python3.5/site-packages/django/core/handlers/base.py", line 187, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/Users/Costantin/GDrive/Analytic.me/dev/venv/lib/python3.5/site-packages/django/core/handlers/base.py", line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/Users/Costantin/GDrive/Analytic.me/dev/venv/lib/python3.5/site-packages/django/views/decorators/csrf.py", line 58, in wrapped_view
    return view_func(*args, **kwargs)
  File "/Users/Costantin/GDrive/Analytic.me/dev/backend/apps/seo_app/views.py", line 152, in webhook
    metrics.parent_key = parent,
  File "/Users/Costantin/GDrive/Analytic.me/dev/venv/lib/python3.5/site-packages/django/db/models/fields/related_descriptors.py", line 216, in __set__
    self.field.remote_field.model._meta.object_name,
ValueError: Cannot assign "(<SeoKeys: SeoKeys object>,)": "SeoMetrics.parent_key" must be a "SeoKeys" instance.
Ignacio Vazquez-Abrams
2#
Ignacio Vazquez-Abrams Reply to 2017-09-14 02:26:04Z
 File "/Users/Costantin/GDrive/Analytic.me/dev/backend/apps/seo_app/views.py", line 152, in webhook
   metrics.parent_key = parent,

This code is creating a tuple. Remove the comma at the end of the line.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.31025 second(s) , Gzip On .

© 2016 Powered by cudou.com design MATCHINFO