ValueError: too many values to unpack in Django models - python

Test.py
The files serves to test everything within the memepost app.
from django.test import TestCase
from memepost.models import memepost, memepostForm
from datetime import date
import logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
class memepostTestCase(TestCase):
def setUp(self):
memepost.objects.create(title="foo",
model_pic="/site_media/media/pic_folder/casual_headshot.jpg",
date=date(2017, 05, 14))
memepost.objects.create(title="bar",
date=date(2017,05, 1))
def test_memepost_enters_database(self):
foo = memepost.objects.get("foo")
bar = memepost.objects.get("bar")
self.assertEqual(foo.date, "6/15/17")
logger.debug("foo date is equal to %s", foo.date)
self.assertEqual(bar.model_pic, "no-img.jpg")
logger.debug("bar is equal to %s", bar.model_pic)
models.py
This file serves to be scheme for creating memes in my database.
from __future__ import unicode_literals
from django.forms import ModelForm
from django.db import models
# Create your models here.
class memepost(models.Model):
title = models.CharField(max_length=140)
model_pic = models.ImageField(upload_to='pic_folder/', default='pic_folder/no-img.jpg')
date = models.DateTimeField()
def __unicode__(self):
return self.title
class memepostForm(ModelForm):
class Meta:
model = memepost
fields = ["title", "model_pic", "date"]
error
Okay, so I am getting this warning, but I think the problem has something to do with the title paramater since I call a get function by the title. I hope I was clear.
WARNING:py.warnings:/home/ubuntu/workspace/mysiteenv/local/lib/python2.7/site-packages/django/db/models/fields/__init__.py:1393:
RuntimeWarning: DateTimeField memepost.date received a naive datetime (2017-05-01 00:00:00) while time zone support is active.
RuntimeWarning)
======================================================================
ERROR: test_memepost_enters_database (memepost.tests.memepostTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/ubuntu/workspace/mysite/memepost/tests.py", line 20, in test_memepost_enters_database
foo = memepost.objects.get("foo")
File "/home/ubuntu/workspace/mysiteenv/local/lib/python2.7/site-packages/django/db/models/manager.py", line 122, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/home/ubuntu/workspace/mysiteenv/local/lib/python2.7/site-packages/django/db/models/query.py", line 378, in get
clone = self.filter(*args, **kwargs)
File "/home/ubuntu/workspace/mysiteenv/local/lib/python2.7/site-packages/django/db/models/query.py", line 790, in filter
return self._filter_or_exclude(False, *args, **kwargs)
File "/home/ubuntu/workspace/mysiteenv/local/lib/python2.7/site-packages/django/db/models/query.py", line 808, in _filter_or_exclude
clone.query.add_q(Q(*args, **kwargs))
File "/home/ubuntu/workspace/mysiteenv/local/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1243, in add_q
clause, _ = self._add_q(q_object, self.used_aliases)
File "/home/ubuntu/workspace/mysiteenv/local/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1269, in _add_q
allow_joins=allow_joins, split_subq=split_subq,
File "/home/ubuntu/workspace/mysiteenv/local/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1146, in build_filter
arg, value = filter_expr
ValueError: too many values to unpack
----------------------------------------------------------------------
Ran 1 test in 0.007s

You are not providing keyword argument for get manager method
def test_memepost_enters_database(self):
foo = memepost.objects.get(title="foo")
bar = memepost.objects.get(title="bar")
...

Related

django-import-export | issue in importing data to database | ValueError: Field 'id' expected a number but got ''

I am using Django-import-export for importing data but facing an error given below.
i am using django 4.0.6 python 3.10.5 with PostgreSql and most latest version of django import export
Code settings i tried to import data to postgresql database by django-import-export
class MemberResource(resources.ModelResource):
Brand=Field()
class Meta:
model = model
fields=('id','title','Model_code','Chipset','chipset_description','image','Brand','Cat')
export_order=('id','title','Model_code','Chipset','chipset_description','image','Brand','Cat')
def dehydrate_Brand(self, obj):
return str(obj.Brand.title)
class modelAdmin(ImportExportModelAdmin):
resource_class = MemberResource
list_display=['id','title','Model_code','Chipset','chipset_description','Brand','categories']
search_fields = ['title','Model_code','Chipset',]
fields=('title','Model_code','Chipset','chipset_description','image','Brand','Cat')
admin.site.register(model,modelAdmin)
and got below error also attached the image where i exported the data from app and then edited the same and tried to import and stuck with below error.
[Line number: 1 - Field 'id' expected a number but got ''.
2, f9, sd, gf, kjkj, images/sample_3pahsfV.jfif, 1, 1
Traceback (most recent call last):
File "C:\Users\gsminfinity\Desktop\Master\venv\lib\site-packages\django\db\models\fields\related_descriptors.py", line 187, in _get_
rel_obj = self.field.get_cached_value(instance)
File "C:\Users\gsminfinity\Desktop\Master\venv\lib\site-packages\django\db\models\fields\mixins.py", line 15, in get_cached_value
return instance._state.fields_cache\[cache_name\]
KeyError: 'Brand'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\gsminfinity\Desktop\Master\venv\lib\site-packages\django\db\models\fields\_init_.py", line 1988, in get_prep_value
return int(value)
ValueError: invalid literal for int() with base 10: ''
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\gsminfinity\Desktop\Master\venv\lib\site-packages\import_export\resources.py", line 707, in import_row
diff = self.get_diff_class()(self, original, new)
File "C:\Users\gsminfinity\Desktop\Master\venv\lib\site-packages\import_export\resources.py", line 241, in _init_
self.left = self._export_resource_fields(resource, instance)
File "C:\Users\gsminfinity\Desktop\Master\venv\lib\site-packages\import_export\resources.py", line 262, in _export_resource_fields
return \[resource.export_field(f, instance) if instance else "" for f in resource.get_user_visible_fields()\]
File "C:\Users\gsminfinity\Desktop\Master\venv\lib\site-packages\import_export\resources.py", line 262, in <listcomp>
return \[resource.export_field(f, instance) if instance else "" for f in resource.get_user_visible_fields()\]
File "C:\Users\gsminfinity\Desktop\Master\venv\lib\site-packages\import_export\resources.py", line 919, in export_field
return method(obj)
File "C:\Users\gsminfinity\Desktop\Master\admin\firmApp\admin.py", line 20, in dehydrate_Brand
return str(obj.Brand.title)
File "C:\Users\gsminfinity\Desktop\Master\venv\lib\site-packages\django\db\models\fields\related_descriptors.py", line 205, in _get_
rel_obj = self.get_object(instance)
File "C:\Users\gsminfinity\Desktop\Master\venv\lib\site-packages\django\db\models\fields\related_descriptors.py", line 168, in get_object
return qs.get(self.field.get_reverse_related_filter(instance))
File "C:\Users\gsminfinity\Desktop\Master\venv\lib\site-packages\django\db\models\query.py", line 482, in get
clone = self._chain() if self.query.combinator else self.filter(*args, **kwargs)
File "C:\Users\gsminfinity\Desktop\Master\venv\lib\site-packages\django\db\models\query.py", line 1071, in filter
return self._filter_or_exclude(False, args, kwargs)
File "C:\Users\gsminfinity\Desktop\Master\venv\lib\site-packages\django\db\models\query.py", line 1089, in _filter_or_exclude
clone._filter_or_exclude_inplace(negate, args, kwargs)
File "C:\Users\gsminfinity\Desktop\Master\venv\lib\site-packages\django\db\models\query.py", line 1096, in _filter_or_exclude_inplace
self._query.add_q(Q(*args, **kwargs))
File "C:\Users\gsminfinity\Desktop\Master\venv\lib\site-packages\django\db\models\sql\query.py", line 1502, in add_q
clause, _ = self._add_q(q_object, self.used_aliases)
File "C:\Users\gsminfinity\Desktop\Master\venv\lib\site-packages\django\db\models\sql\query.py", line 1532, in _add_q
child_clause, needed_inner = self.build_filter(
File "C:\Users\gsminfinity\Desktop\Master\venv\lib\site-packages\django\db\models\sql\query.py", line 1358, in build_filter
return self._add_q(
File "C:\Users\gsminfinity\Desktop\Master\venv\lib\site-packages\django\db\models\sql\query.py", line 1532, in _add_q
child_clause, needed_inner = self.build_filter(
File "C:\Users\gsminfinity\Desktop\Master\venv\lib\site-packages\django\db\models\sql\query.py", line 1448, in build_filter
condition = self.build_lookup(lookups, col, value)
File "C:\Users\gsminfinity\Desktop\Master\venv\lib\site-packages\django\db\models\sql\query.py", line 1273, in build_lookup
lookup = lookup_class(lhs, rhs)
File "C:\Users\gsminfinity\Desktop\Master\venv\lib\site-packages\django\db\models\lookups.py", line 27, in _init_
self.rhs = self.get_prep_lookup()
File "C:\Users\gsminfinity\Desktop\Master\venv\lib\site-packages\django\db\models\lookups.py", line 85, in get_prep_lookup
return self.lhs.output_field.get_prep_value(self.rhs)
File "C:\Users\gsminfinity\Desktop\Master\venv\lib\site-packages\django\db\models\fields\_init_.py", line 1990, in get_prep_value
raise e._class_(
ValueError: Field 'id' expected a number but got ''.][1]
the issue was dehydrating foreign key gives only read-only data which was for the purpose of export only to import we need to use the widgets which will resolve the foreign keys and import the data to database.
Link to Documentation
class MemberResource(resources.ModelResource):
Brand = fields.Field(
column_name='Brand',
attribute='Brand',
widget=ForeignKeyWidget(brand, 'title'))
class Meta:
model = model
fields=('id','title','Model_code','Chipset','chipset_description','image','Brand','Cat')
That means that there are missing values in the "id" column.
I don't know "Django import / export", sounds very good. An old way of mine with any import/export is to export some sample data and then open it in excel or pandas. Because of the following:
You have all the required columns.
The columns are in the correct order.
And if you have test data you can see the data-types of each column.
Take care using Excel because it changes True into TRUE and generates weird date formats.
Another issue is that after importing django u must also be aware of the last "id" number in order to give the correct AutoIncrement for a new record. That's usually corrected with SQL, but maybe "Django-import-export" corrects this automatically.

Django-PolymorphicModels - Error when deleting an instance

It's the first time I'm trying 'PolymorphicModel', maybe there is something I'm doing wrong or I'm trying to do something that is not supported. Is there anyway to get around this?
My simplified model:
from django.polymorphic import PolymorphicModel
class Question(PolymorphicModel):
description = models.TextField(default='')
compound_question = models.ForeignKey('CompoundQuestion',
on_delete=models.CASCADE,
blank=True,
null=True,
related_name='sub_questions')
bonus_question = models.OneToOneField('BonusQuestion',
on_delete=models.CASCADE,
blank=True,
null=True,
related_name='question')
class SimpleQuestion(Question):
pass
class CompoundQuestion(Question):
pass
class BonusQuestion(Question):
pass
If I create the necessary instances,
compound_q = CompoundQuestion()
compound_q.save()
simple_q2 = SimpleQuestion(compound_question=compound_q)
simple_q2.save()
bonus_q = BonusQuestion(compound_question=compound_q)
bonus_q.save()
simple_q1 = SimpleQuestion(bonus_question=bonus_q)
simple_q1.save()
so that in the end I end up with:
compound_q.sub_questions.all()
>>> <PolymorphicQuerySet [<BonusQuestion: BonusQuestion object (5)>, <SimpleQuestion: SimpleQuestion object (8)>]>
After I do compound_q.delete()
I get:
Traceback (most recent call last):
File "<console>", line 1, in <module>
File ".../Python/Django/env/lib/python3.6/site-packages/django/db/models/base.py", line 936, in delete
collector.collect([self], keep_parents=keep_parents)
File ".../Python/Django/env/lib/python3.6/site-packages/django/db/models/deletion.py", line 245, in collect
field.remote_field.on_delete(self, field, sub_objs, self.using)
File ".../Python/Django/env/lib/python3.6/site-packages/django/db/models/deletion.py", line 17, in CASCADE
source_attr=field.name, nullable=field.null)
File ".../Python/Django/env/lib/python3.6/site-packages/django/db/models/deletion.py", line 226, in collect
sub_objs = self.related_objects(related, batch)
File ".../Python/Django/env/lib/python3.6/site-packages/django/db/models/deletion.py", line 257, in related_objects
**{"%s__in" % related.field.name: objs}
File ".../Python/Django/env/lib/python3.6/site-packages/django/db/models/query.py", line 904, in filter
return self._filter_or_exclude(False, *args, **kwargs)
File ".../Python/Django/env/lib/python3.6/site-packages/polymorphic/query.py", line 173, in _filter_or_exclude
negate, *(list(q_objects) + additional_args), **kwargs
File ".../Python/Django/env/lib/python3.6/site-packages/django/db/models/query.py", line 923, in _filter_or_exclude
clone.query.add_q(Q(*args, **kwargs))
File ".../Python/Django/env/lib/python3.6/site-packages/django/db/models/sql/query.py", line 1340, in add_q
clause, _ = self._add_q(q_object, self.used_aliases)
File ".../Python/Django/env/lib/python3.6/site-packages/django/db/models/sql/query.py", line 1371, in _add_q
check_filterable=check_filterable,
File ".../Python/Django/env/lib/python3.6/site-packages/django/db/models/sql/query.py", line 1274, in build_filter
self.check_related_objects(join_info.final_field, value, join_info.opts)
File ".../Python/Django/env/lib/python3.6/site-packages/django/db/models/sql/query.py", line 1115, in check_related_objects
self.check_query_object_type(v, opts, field)
File ".../Python/Django/env/lib/python3.6/site-packages/django/db/models/sql/query.py", line 1095, in check_query_object_type
(value, opts.object_name))
ValueError: Cannot query "SimpleQuestion object (8)": Must be "BonusQuestion" instance.
Is there something wrong with my model? Everything else seems to be working as expected.
Thanks for reading :)
This appears to be a known bug in the django-polymorphic package. There is a work around suggested here to add a vanilla Manager to your class and to set this manager as the default
class Question(PolymorphicModel):
non_polymorphic = models.Manager()
class Meta
base_manager_name = 'non_polymorphic'

I got this "ValueError: invalid literal for int() with base 10" in my Django project [duplicate]

This question already has answers here:
ValueError: invalid literal for int() with base 10: ''
(15 answers)
Closed 5 years ago.
model in my application:
from __future__ import unicode_literals
from django.db import models
class Category(models.Model):
name = models.CharField(max_length = 128, unique = True)
def __unicode__(self):
return self.name
class ModelName(models.Model):
name = models.CharField(max_length = 128, unique = True)
def __unicode__(self):
return self.name
class CarStorage(models.Model):
category = models.ForeignKey(Category)
CarID = models.CharField(max_length = 10, unique = True, null = False)
model = models.ForeignKey(ModelName)
storage = models.IntegerField(default = 0)
perchasedYear = models.IntegerField(default = 0)
def __unicode__(self):
return self.CarID
Population code:
import os
os.environ.setdefault('DJANGO_SETTINGS_MODULE','TKTrading.settings')
import django
django.setup()
from APP.models import Category, ModelName, CarStorage
def populate():
BASE_DIR = '/Users/apple/WebWorkshop/TKTrading/'
DATA_DIR = os.path.join(BASE_DIR, 'APP/Data')
CATEGORY = open(os.path.join(DATA_DIR, 'CATEGORY.txt'),'r')
global CategoryList
CategoryList = []
for line in CATEGORY:
newC = Add_Category(line)
CategoryList.append(newC)
CATEGORY.close
MODELNAME = open(os.path.join(DATA_DIR, 'Model.txt'), 'r')
global ModelList
ModelList = []
for line2 in MODELNAME:
newM = Add_ModelName(line2)
ModelList.append(newM)
MODELNAME.close
CARSTORAGE = open(os.path.join(DATA_DIR, 'CARSTORAGE.txt'), 'r')
for line3 in CARSTORAGE:
print(line3)
c = line3.split(',')
ID = str(c[0])
category = str(c[1])
model = str(c[2])
storage = int(c[3])
year = int(c[4])
Add_CarStorage(ID, category, model, storage, year)
CARSTORAGE.close
def Add_Category(name):
c = Category.objects.get_or_create(name = name)[0]
c.save()
return c
def Add_ModelName(name):
m = ModelName.objects.get_or_create(name = name)[0]
m.save()
return m
def Add_CarStorage(ID, category, model, storage, year):
for CATEGORY in CategoryList:
if CATEGORY.name == category:
category = CATEGORY
for MODEL in ModelList:
if MODEL.name == model:
model = MODEL
car = CarStorage.objects.get_or_create(CarID = ID,
category = category,
model = model,)[0]
car.storage = storage
car.perchasedYear = year
car.save()
return car
if __name__ =='__main__':
print('Starting population...')
populate()
For the population code, I have try two way but got the same message.
One is put all the category and model in a list as object, the other one is using string to create new object directly.
In the end this is the whole message feedback:
Traceback (most recent call last):
File "DBPopulation.py", line 77, in <module>
populate()
File "DBPopulation.py", line 45, in populate
Add_CarStorage(ID, category, model, storage, year)
File "DBPopulation.py", line 69, in Add_CarStorage
model = model,)[0]
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/query.py", line 471, in get_or_create
return self.get(**lookup), False
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/query.py", line 376, in get
clone = self.filter(*args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/query.py", line 794, in filter
return self._filter_or_exclude(False, *args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/query.py", line 812, in _filter_or_exclude
clone.query.add_q(Q(*args, **kwargs))
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1227, in add_q
clause, _ = self._add_q(q_object, self.used_aliases)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1253, in _add_q
allow_joins=allow_joins, split_subq=split_subq,
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1183, in build_filter
condition = lookup_class(lhs, value)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/lookups.py", line 19, in __init__
self.rhs = self.get_prep_lookup()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/fields/related_lookups.py", line 100, in get_prep_lookup
self.rhs = target_field.get_prep_value(self.rhs)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 946, in get_prep_value
return int(value)
ValueError: invalid literal for int() with base 10: 'Wing'
TXT file:
2512-305,Wing,Nissan UD,10,2003
2010-41,Wing,Hino,4,1997
2607-312,Cargo Drop Side,Isuzu,6,2012
2411-341,Cargo Drop Side,Nissan UD,7,2000
2406-326,Tractor,Nissan UD,12,1996
2211-101,Other,Komatsu,1,0
I understand there might have problems like putting string or nothing in the integer field, but I have reread many time and still not figure out.
The problem is with your Add_CarStorage function.
when
if MODEL.name == model:
is true you are replacing string model with MODEL which is a ModelName instance but when the condition is false you are not updating the variable model with anything. A quick fix would be to use get_or_create call only when the condition is true.

Why does this very basic query on this Django model fail?

I have the following Django class:
from caching.base import CachingManager, CachingMixin
from mptt.models import MPTTModel
def make_id():
'''
inspired by http://instagram-engineering.tumblr.com/post/10853187575/sharding-ids-at-instagram
'''
START_TIME = 1876545318034
return (int(time.time()*1000) - START_TIME << 23 ) | random.SystemRandom().getrandbits(23)
class My_Class(CachingMixin, MPTTModel):
id = models.BigIntegerField(default=make_id, primary_key=True)
# Other Attributes Snipped here for brevity
But look what happens when I try to query this class:
>>> My_Class.objects.get(pk=5000)
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "my_virtual_env/lib/python2.7/site-packages/django/db/models/manager.py", line 127, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "my_virtual_env/lib/python2.7/site-packages/django/db/models/query.py", line 334, in get
self.model._meta.object_name
DoesNotExist: My_Class matching query does not exist.
Why does it fail?? How can I fix this?
It could be you have no My_Class with id = 5000
try:
mc = My_Class.objects.get(pk=id)
except My_Class.DoesNotExist:
mc = None

ValueError: Attribute Users.request is required

I need help again. Can anybody tell my why I get this error? I use Flask and pony orm and python 3.4. I have few files.
users.py
from app import models
from pony.orm import *
class UsersAPI:
#staticmethod
#db_session
def is_registered(e_mail):
"""
This method for checking registered user or not
:param e_mail:
:return:
"""
user = select(o for o in models.Users if o.email == e_mail)[:]
print(user)
UsersAPI.is_registered("evgenusov96#yandex.ru")
File models.py
from pony.orm import *
import config
db = Database()
class Users(db.Entity):
id = PrimaryKey(int, auto=True)
email = Required(str, 100)
password = Required(str, 32)
name = Required(str, 15)
surname = Required(str, 20)
city = Required(City, nullable=True)
sex = Required(int)
request = Required(str, 250, nullable=True)
hobby = Required(str, 250, nullable=True)
about = Required(str, 250, nullable=True)
status_message = Required(str, 100, nullable=True)
user1 = Set("Friends", reverse="user1")
user2 = Set("Friends", reverse="user2")
author = Set("Messages", reverse="author")
addressee = Set("Messages", reverse="addressee")
This error I got:
C:\Python34\python.exe C:/Users/Евгений/PycharmProjects/langunet/run.py
Traceback (most recent call last):
File "C:/Users/Евгений/PycharmProjects/langunet/run.py", line 2, in <module>
from app import app
File "C:\Users\Евгений\PycharmProjects\langunet\app\__init__.py", line 4, in <module>
from app import routes
File "C:\Users\Евгений\PycharmProjects\langunet\app\routes.py", line 2, in <module>
from app.users import *
File "C:\Users\Евгений\PycharmProjects\langunet\app\users.py", line 21, in <module>
UsersAPI.is_registered("evgenusov96#yandex.ru")
File "<string>", line 2, in is_registered
File "C:\Python34\lib\site-packages\pony\orm\core.py", line 347, in new_func
try: return func(*args, **kwargs)
File "C:\Users\Евгений\PycharmProjects\langunet\app\users.py", line 18, in is_registered
user = get(o for o in models.Users if o.email == e_mail)
File "<string>", line 2, in get
File "C:\Python34\lib\site-packages\pony\utils.py", line 88, in cut_traceback
return func(*args, **kwargs)
File "C:\Python34\lib\site-packages\pony\orm\core.py", line 4260, in get
return make_query(args, frame_depth=3).get()
File "<string>", line 2, in get
File "C:\Python34\lib\site-packages\pony\utils.py", line 88, in cut_traceback
return func(*args, **kwargs)
File "C:\Python34\lib\site-packages\pony\orm\core.py", line 4531, in get
objects = query[:2]
File "<string>", line 2, in __getitem__
File "C:\Python34\lib\site-packages\pony\utils.py", line 88, in cut_traceback
return func(*args, **kwargs)
File "C:\Python34\lib\site-packages\pony\orm\core.py", line 4721, in __getitem__
return query._fetch(range=(start, stop))
File "C:\Python34\lib\site-packages\pony\orm\core.py", line 4426, in _fetch
used_attrs=translator.get_used_attrs())
File "C:\Python34\lib\site-packages\pony\orm\core.py", line 3228, in _fetch_objects
real_entity_subclass, pkval, avdict = entity._parse_row_(row, attr_offsets)
File "C:\Python34\lib\site-packages\pony\orm\core.py", line 3258, in _parse_row_
avdict[attr] = attr.parse_value(row, offsets)
File "C:\Python34\lib\site-packages\pony\orm\core.py", line 1329, in parse_value
val = attr.validate(row[offset], None, attr.entity, from_db=True)
File "C:\Python34\lib\site-packages\pony\orm\core.py", line 1611, in validate
if obj is None: throw(ValueError, 'Attribute %s is required' % attr)
File "C:\Python34\lib\site-packages\pony\utils.py", line 126, in throw
raise exc
ValueError: Attribute Users.request is required
How this may to fix? Thank you!
This error can happen if you defined User.request attribute as Required, but in the table the corresponding column is actually optional (that is, nullable). During loading of an object, Pony checks if the object's internal state is consistent. Since the value of a required attribute is missed, Pony generates an exception.
I think that we need to improve Pony behavior in one of two ways here:
Even if an attribute is required, Pony should allow loading of null values for this attribute from the database.
Another alternative is to make error message more clear.

Categories

Resources