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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With