0

I was trying to access part of the model self attributes, however after migrations this error showed up.

The model works perfectly fine before I ran makemigrations and migrate.

After reading through the value error page, I notice that the value error was cause by the model trying to access it's own foreign key(choosen_room_1) under a property function.

  Traceback:
  File "/Users/theblackcat/website/env/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  132.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/Users/theblackcat/website/backend/views.py" in dashboard
  54.       return render_to_response('form/dashboard.html', RequestContext(request, locals()))
  File "/Users/theblackcat/website/env/lib/python2.7/site-packages/django/shortcuts.py" in render_to_response
  39.         content = loader.render_to_string(template_name, context, using=using)
  File "/Users/theblackcatwebsite/env/lib/python2.7/site-packages/django/template/loader.py" in render_to_string
  99.         return template.render(context, request)
  File "/Users/theblackcat/website/env/lib/python2.7/site-packages/django/template/backends/django.py" in render
  74.         return self.template.render(context)
  File "/Users/theblackcat/website/env/lib/python2.7/site-packages/django/template/base.py" in render
  210.                     return self._render(context)
  File "/Users/theblackcat/website/env/lib/python2.7/site-packages/django/template/base.py" in _render
  202.         return self.nodelist.render(context)
  File "/Users/theblackcat/website/env/lib/python2.7/site-packages/django/template/base.py" in render
  905.                 bit = self.render_node(node, context)      
  File "/Users/theblackcat/website/env/lib/python2.7/site-packages/django/template/debug.py" in render_node
  79.             return node.render(context)
  File "/Users/theblackcat/website/env/lib/python2.7/site-packages/django/template/loader_tags.py" in render
  135.         return compiled_parent._render(context)
  File "/Users/theblackcat/website/env/lib/python2.7/site-packages/django/template/base.py" in _render
202.         return self.nodelist.render(context)
  File "/Users/theblackcat/website/env/lib/python2.7/site-packages/django/template/base.py" in render
905.                 bit = self.render_node(node, context)
  File "/Users/theblackcat/website/env/lib/python2.7/site-packages/django/template/debug.py" in render_node
79.             return node.render(context)
  File "/Users/theblackcat/website/env/lib/python2.7/site-packages/django/template/loader_tags.py" in render
65.                 result = block.nodelist.render(context)
  File "/Users/theblackcat/website/env/lib/python2.7/site-packages/django/template/base.py" in render
905.                 bit = self.render_node(node, context)
  File "/Users/theblackcat/website/env/lib/python2.7/site-packages/django/template/debug.py" in render_node
79.             return node.render(context)
  File "/Users/theblackcat/website/env/lib/python2.7/site-packages/django/template/defaulttags.py" in render
329.                 return nodelist.render(context)
  File "/Users/theblackcat/website/env/lib/python2.7/site-packages/django/template/base.py" in render
905.                 bit = self.render_node(node, context)
  File "/Users/theblackcat/website/env/lib/python2.7/site-packages/django/template/debug.py" in render_node
79.             return node.render(context)
  File "/Users/theblackcat/website/env/lib/python2.7/site-packages/django/template/defaulttags.py" in render
217.                             nodelist.append(node.render(context))
  File "/Users/theblackcat/website/env/lib/python2.7/site-packages/django/template/debug.py" in render
89.             output = self.filter_expression.resolve(context)
  File "/Users/theblackcat/website/env/lib/python2.7/site-packages/django/template/base.py" in resolve
648.                 obj = self.var.resolve(context)
  File "/Users/theblackcat/website/env/lib/python2.7/site-packages/django/template/base.py" in resolve
789.             value = self._resolve_lookup(context)
  File "/Users/theblackcat/website/env/lib/python2.7/site-packages/django/template/base.py" in _resolve_lookup
827.                         current = getattr(current, bit)
  File "/Users/theblackcat/website_koref/backend/models.py" in total_price
226.        price,_ = self.getTotalPricing()
  File "/Users/theblackcat/website/backend/models.py" in getTotalPricing
212.        if self.choosen_room_1 is not None:
  File "/Users/theblackcat/website/env/lib/python2.7/site-packages/django/db/models/fields/related.py" in __get__
610.                     qs = qs.filter(**params)
  File "/Users/theblackcat/website/env/lib/python2.7/site-packages/django/db/models/query.py" in filter
679.         return self._filter_or_exclude(False, *args, **kwargs)
  File "/Users/theblackcat/website/env/lib/python2.7/site-packages/django/db/models/query.py" in _filter_or_exclude
697.             clone.query.add_q(Q(*args, **kwargs))      
  File "/Users/theblackcat/website/env/lib/python2.7/site-packages/django/db/models/sql/query.py" in add_q
1310.         clause, require_inner = self._add_q(where_part, self.used_aliases)
  File "/Users/theblackcat/website/env/lib/python2.7/site-packages/django/db/models/sql/query.py" in _add_q
1338.                     allow_joins=allow_joins, split_subq=split_subq,
  File "/Users/theblackcat/website/env/lib/python2.7/site-packages/django/db/models/sql/query.py" in build_filter
1209.             condition = self.build_lookup(lookups, col, value)
  File "/Users/theblackcat/website/env/lib/python2.7/site-packages/django/db/models/sql/query.py" in build_lookup
1102.                 return final_lookup(lhs, rhs)
  File "/Users/theblackcat/website/env/lib/python2.7/site-packages/django/db/models/lookups.py" in __init__
105.         self.rhs = self.get_prep_lookup()
  File "/Users/theblackcat/website/env/lib/python2.7/site-packages/django/db/models/lookups.py" in get_prep_lookup
143.         return self.lhs.output_field.get_prep_lookup(self.lookup_name, self.rhs)
  File "/Users/theblackcat/website/env/lib/python2.7/site-packages/django/db/models/fields/__init__.py" in get_prep_lookup
727.             return self.get_prep_value(value)
  File "/Users/theblackcat/website/env/lib/python2.7/site-packages/django/db/models/fields/__init__.py" in get_prep_value
985.         return int(value)

  Exception Type: ValueError at /en/backend/
  Exception Value: invalid literal for int() with base 10: 'choosen_room_1_id'

models.py

def getTotalPricing(self):
    total_price = 0
    amount = dict()
    if self.choosen_room_1 is not None:

The choosen_room_1 attribute was defined as below:

models.py

choosen_room_1 = models.ForeignKey(ChoosenRoom, default=None, related_name="room_type_1", null=True)

Some things I have tried so far are:

  1. Removed old migrations file

  2. Checked if id field exist in the table using sqlite terminal, and it does exist

  3. Force choosen_room_1 to_field as id or choosenroom_id, same error still exist

  4. Using Django admin interface I am sure the choosenroom wasn't null and was able to reassign it with a new foreign key item.

  5. Accessing choosen_room_1 using manage.py shell will give me the same error.

After reading through some of the stackoverflow issues, my guess was somehow foreignkey was expecting the unique id key with integer type, however something went wrong during query process? As mention here

By the way, I was running this project under Python 2.7 with Django 1.8.18

UPDATE After flushing the database, and took me some time to re populate the database with similar data, it works like a charm now.

0 Answers0