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

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

Related

Error when indexing data using elasticsearch dsl

I have two models which are as follows:
class PostUser(models.Model):
user_id = models.CharField(max_length=1000,blank=True,null=True)
reputation = models.CharField(max_length = 1000 , blank = True , null = True)
def __unicode__(self):
return self.user_id
def indexing(self):
obj = PostUserIndex(
meta = {'id': self.id},
user_id = self.user_id,
reputation = self.reputation,
)
obj.save(index = 'post-user-index')
return obj.to_dict(include_meta=True)
class Posts(models.Model):
user_post_id = models.CharField(max_length = 1000 , blank = True , null = True)
score = models.CharField(max_length = 1000 , blank = True , null = True)
owner_user_id = models.ForeignKey(PostUser,default="-100")
def __unicode__(self):
return self.user_post_id
def indexing(self):
obj = PostsIndex(
meta = {'id': self.id},
user_post_id = self.user_post_id,
score = self.score,
owner_user_id = self.owner_user_id,
)
obj.save(index = 'newuserposts-index')
return obj.to_dict(include_meta=True)
The way I am trying to index my data is as follows:
class PostUserIndex(DocType):
user_id = Text()
reputation = Text()
class PostsIndex(DocType):
user_post_id = Text()
score = Text()
owner_user_id = Nested(PostUserIndex)
Then i try to run the following method to index data:
def posts_indexing():
PostsIndex.init(index='newuserposts-index')
es = Elasticsearch()
bulk(client=es, actions=(b.indexing() for b in models.Posts.objects.all().iterator()))
I have tried different approaches by manually entering the nested properties and also changing from doctype to inner doc of PostUser but still I am getting the weird error.
ERROR:
AttributeError: 'PostUser' object has no attribute 'copy'
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/Users/ammarkhan/Desktop/danny/src/dataquerying/datatoindex.py", line 74, in new_user_posts_indexing
bulk(client=es, actions=(b.indexing() for b in models.Posts.objects.all().iterator()))
File "/Users/ammarkhan/Desktop/danny/lib/python2.7/site-packages/elasticsearch/helpers/__init__.py", line 257, in bulk
for ok, item in streaming_bulk(client, actions, **kwargs):
File "/Users/ammarkhan/Desktop/danny/lib/python2.7/site-packages/elasticsearch/helpers/__init__.py", line 180, in streaming_bulk
client.transport.serializer):
File "/Users/ammarkhan/Desktop/danny/lib/python2.7/site-packages/elasticsearch/helpers/__init__.py", line 58, in _chunk_actions
for action, data in actions:
File "/Users/ammarkhan/Desktop/danny/src/dataquerying/datatoindex.py", line 74, in <genexpr>
bulk(client=es, actions=(b.indexing() for b in models.Posts.objects.all().iterator()))
File "/Users/ammarkhan/Desktop/danny/src/dataquerying/models.py", line 167, in indexing
obj.save(index = 'newuserposts-index')
File "/Users/ammarkhan/Desktop/danny/lib/python2.7/site-packages/elasticsearch_dsl/document.py", line 405, in save
self.full_clean()
File "/Users/ammarkhan/Desktop/danny/lib/python2.7/site-packages/elasticsearch_dsl/utils.py", line 417, in full_clean
self.clean_fields()
File "/Users/ammarkhan/Desktop/danny/lib/python2.7/site-packages/elasticsearch_dsl/utils.py", line 403, in clean_fields
data = field.clean(data)
File "/Users/ammarkhan/Desktop/danny/lib/python2.7/site-packages/elasticsearch_dsl/field.py", line 179, in clean
data = super(Object, self).clean(data)
File "/Users/ammarkhan/Desktop/danny/lib/python2.7/site-packages/elasticsearch_dsl/field.py", line 90, in clean
data = self.deserialize(data)
File "/Users/ammarkhan/Desktop/danny/lib/python2.7/site-packages/elasticsearch_dsl/field.py", line 86, in deserialize
return self._deserialize(data)
File "/Users/ammarkhan/Desktop/danny/lib/python2.7/site-packages/elasticsearch_dsl/field.py", line 166, in _deserialize
return self._wrap(data)
File "/Users/ammarkhan/Desktop/danny/lib/python2.7/site-packages/elasticsearch_dsl/field.py", line 142, in _wrap
return self._doc_class.from_es(data)
File "/Users/ammarkhan/Desktop/danny/lib/python2.7/site-packages/elasticsearch_dsl/utils.py", line 342, in from_es
meta = hit.copy()
AttributeError: ‘PostUser' object has no attribute 'copy'
You are calling .save in your indexing methods which will save the document to elasticsearch and then you are also passing it to bulk to accomplish the same, the save is extra.
You are also assigning an instance of PostUser to owner_user_id instead of properly serializing it by calling the indexing method on it (without the save inside):
def indexing(self):
obj = PostsIndex(
meta = {'id': self.id, 'index': 'newuserposts-index'},
user_post_id = self.user_post_id,
score = self.score,
owner_user_id = self.owner_user_id.indexing(),
)
return obj.to_dict(include_meta=True)

factory boy cannot inherit from base factory with metaclass: `issubclass` error

I'm fairly new to factory boy. I've got a model that gets created in different places in an object hierarchy, and with different attributes, depending on how it is used. In this case I have a picture that is either a logo or a profile pic.
This works fine:
class ProfileImageBase(factory.Factory):
mimetype = 'png'
image = 'some bytes'
class UserProfileImage(ProfileImageBase):
class Meta:
model = models.media_models.ProfileImage
parent = factory.SubFactory(BasicUser)
id = ProfileImage.PROFILE
class MerchantLogoImage(ProfileImageBase):
class Meta:
model = models.media_models.ProfileImage
parent = factory.SubFactory(BasicMerchant)
id = ProfileImage.LOGO
Not very DRY eh? I'd like to put the Meta class in the base class, but this gives me crazy errors:
class ProfileImageBase(factory.Factory):
class Meta:
model = models.media_models.ProfileImage
mimetype = 'png'
image = 'some bytes'
class UserProfileImage(ProfileImageBase):
parent = factory.SubFactory(BasicUser)
id = ProfileImage.PROFILE
class MerchantLogoImage(ProfileImageBase):
parent = factory.SubFactory(BasicMerchant)
id = ProfileImage.LOGO
Here's the traceback
Traceback (most recent call last):
File "/home/sheena/Workspace/Waxed/venvs/wxt_comp/local/lib/python2.7/site-packages/nose/loader.py", line 418, in loadTestsFromName
addr.filename, addr.module)
File "/home/sheena/Workspace/Waxed/venvs/wxt_comp/local/lib/python2.7/site-packages/nose/importer.py", line 47, in importFromPath
return self.importFromDir(dir_path, fqname)
File "/home/sheena/Workspace/Waxed/venvs/wxt_comp/local/lib/python2.7/site-packages/nose/importer.py", line 94, in importFromDir
mod = load_module(part_fqname, fh, filename, desc)
File "/home/sheena/Workspace/Waxed/code/waxed_backend/src/waxed_backend/concerns/pictures/tests/view_tests.py", line 3, in <module>
from ....models.test_factories.profile_pictures import MerchantLogoImage
File "/home/sheena/Workspace/Waxed/code/waxed_backend/src/waxed_backend/models/test_factories/profile_pictures.py", line 13, in <module>
class UserProfileImage(ProfileImageBase):
File "/home/sheena/Workspace/Waxed/venvs/wxt_comp/local/lib/python2.7/site-packages/factory/base.py", line 114, in __new__
params=attrs_params,
File "/home/sheena/Workspace/Waxed/venvs/wxt_comp/local/lib/python2.7/site-packages/factory/base.py", line 232, in contribute_to_class
self.counter_reference = self._get_counter_reference()
File "/home/sheena/Workspace/Waxed/venvs/wxt_comp/local/lib/python2.7/site-packages/factory/base.py", line 259, in _get_counter_reference
and issubclass(self.model, self.base_factory._meta.model)):
TypeError: Error when calling the metaclass bases
issubclass() arg 1 must be a class
My question is: wht on earth is happening here? I dont believe I'm doing anything novel.

ValueError: too many values to unpack in Django models

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")
...

neomodel giving Attribute error on save

I am running the following config for neo4j:
neo4j - 3.0.0
py2neo - 2.0.8
neomodel - 2.0.2
Finally, the code I try to run is:
class User(neomodel.StructuredNode):
user_id = neomodel.IntegerProperty(unique_index=True, required=True)
name = neomodel.StringProperty()
phone_number = neomodel.StringProperty()
user = User(user_id=6, name='Sourabh Dev', phone_number='9711237840').save()
I don't understand, why I keep getting this strange error. Am I doing something wrong here or should I use py2neo instead of neomodel?
My traceback is:
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "/usr/local/lib/python3.4/site-packages/neomodel-2.0.2-py3.4.egg/neomodel/signals.py", line 25, in hooked
val = fn(self, *args, **kwargs)
File "/usr/local/lib/python3.4/site-packages/neomodel-2.0.2-py3.4.egg/neomodel/core.py", line 159, in save
self._id = self.create(self.__properties__)[0]._id
File "/usr/local/lib/python3.4/site-packages/neomodel-2.0.2-py3.4.egg/neomodel/core.py", line 289, in create
results = db.cypher_query(query, params)
File "/usr/local/lib/python3.4/site-packages/neomodel-2.0.2-py3.4.egg/neomodel/util.py", line 213, in cypher_query
results = self._execute_query(query, params)
File "/usr/local/lib/python3.4/site-packages/neomodel-2.0.2-py3.4.egg/neomodel/util.py", line 207, in _execute_query
results = self.session.cypher.execute(query, create_params=params)
File "/usr/local/lib/python3.4/site-packages/py2neo/cypher/core.py", line 136, in execute
results = tx.commit()
File "/usr/local/lib/python3.4/site-packages/py2neo/cypher/core.py", line 333, in commit
return self.post(self.__commit or self.__begin_commit)
File "/usr/local/lib/python3.4/site-packages/py2neo/cypher/core.py", line 288, in post
raise self.error_class.hydrate(error)
File "/usr/local/lib/python3.4/site-packages/py2neo/cypher/error/core.py", line 54, in hydrate
error_cls = getattr(error_module, title)
AttributeError: 'module' object has no attribute 'TypeError'
Two points here:
Existing Neomodel doesn't support the Neo4j 3.0
Cypher syntax has changed in 3.0, so the error raises.
In 2.x , MATCH n RETURN n
In 3.0 , MATCH (n) RETURN n
Node n is enclosed in braces.

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