django-mongokit icon indicating copy to clipboard operation
django-mongokit copied to clipboard

How to access function defined in a class?

Open AvadootNachankar opened this issue 11 years ago • 1 comments

///////////////////////////////////////////////////////////////////////////////////////////////// @connection.register

class Author(DjangoDocument): collection_name = 'Authors' structure = { 'name': unicode, 'created_at': datetime.datetime }

required_fields = ['name']
default_values = {'created_at':datetime.datetime.utcnow}

use_dot_notation = True

def my_func(self):
    print(" my_func working...\n")

/////////////////////////////////////////////////////////////////////////////////////////////////

in shell, i.e. "python manage.py shell"

from .models import * from django_mongokit import get_database from bson import ObjectId

db = get_database()

c_author = db[Author.collection_name]

o_author = c_author.Author()

o_author {'created_at': datetime.datetime(2013, 9, 16, 9, 51, 43, 200898), 'name': None}

o_author.my_func() Traceback (most recent call last): File "", line 1, in File "/home/tissavadoot/Desktop/Tissproject/TP_MK/local/lib/python2.7/site-packages/mongokit/schema_document.py", line 379, in getattr return dict.getattribute(self, key) AttributeError: 'Author' object has no attribute 'my_func'

I'm stuck over here, need help!!

AvadootNachankar avatar Sep 17 '13 09:09 AvadootNachankar

This issue is also resolved by following way:-

we imported following package from django: import django.db import models

Then inside the class:

@connection.register class Author(DjangoDocument):

# Added the below line, and it does the trick for me...
objects = models.Manager()

collection_name = 'Authors'
structure = {
    'name': unicode,
    'created_at': datetime.datetime
}
.
.
.
.
def my_func(self, *args, **kwargs):
    return (" my_func working...\n")

Is it the correct way?? Please suggest.

AvadootNachankar avatar Sep 20 '13 10:09 AvadootNachankar