What does the Django "expected string or buffer" error indicate? - django

I've been stuck with this error for quite a while now and I just can't figure out what it means. It occurs when I try to save an object to my mysql database. Any ideas?
Thanks for the help!

Just ran into the same problem and resolved it. I instantiated a form like this:
data = {'date' : datetime.now} #this is the problem
form = MyForm(data)
That form was saved later on and django tried to set 'date' in the model. But datetime.now refers to a function rather than a date, obviously. What I wanted to do was datetime.now()
Maybe this helps anybody running into this in future.

This probably means that Python is trying to execute code which is expecting a certain datatype (bool, string, int, etc.), but an other, incorrect, datatype is provided.

In my case it was appears when I use "time" library to convert date string to datetime object. I just use "datetime.strptime" instead of "time.strptime"and problem vanished.

The datetime validator in django is in the file:
/path/to/project/venv/lib/python2.7/site-packages/django/utils/dateparse.py
or in the site-packages of your current python interpreter.
Have a look there to see the regular expressions. In my case, the way to solve it was:
ended=datetime.fromtimestamp(time.time())
other=datetime.fromtimestamp(time.time())
# in the model:
ended = models.DateTimeField(blank=True, null=True) # or
other = models.DateTimeField(auto_now_add=False, blank=True)
both working.

'expiration' is this field
expiration = models.DateTimeField(default=7)
And code with error was this one:
ex = timedelta(minutes=expiration)
authobj, created = cls.objects.update_or_create(
operator=operator,
defaults={'transport':transport,'token':cls.generate_key(40),'expiration':ex}
)
And it was solved by setting it like this (instead of timedelta, a date)
ex = datetime.now() + timedelta(minutes=expiration)
authobj, created = cls.objects.update_or_create(
operator=operator,
defaults={'transport':transport,'token':cls.generate_key(40),'expiration':ex}
)

str(yourvar)
Ou can convert sting your variables.

Related

ElasticSearch - bulk indexing for a completion suggester in python

I am trying to add a completion suggester to enable search-as-you-type for a search field in my Django app (using Elastic Search 5.2.x and elasticseach-dsl). After trying to figure this out for a long time, I am not able to figure yet how to bulk index the suggester. Here's my code:
class SchoolIndex(DocType):
name = Text()
school_type = Keyword()
name_suggest = Completion()
Bulk indexing as follows:
def bulk_indexing():
SchoolIndex.init(index="school_index")
es = Elasticsearch()
bulk(client=es, actions=(a.indexing() for a in models.School.objects.all().iterator()))
And have defined an indexing method in models.py:
def indexing(self):
obj = SchoolIndex(
meta = {'id': self.pk},
name = self.name,
school_type = self.school_type,
name_suggest = {'input': self.name } <--- # what goes in here?
)
obj.save(index="school_index")
return obj.to_dict(include_meta=True)
As per the ES docs, suggestions are indexed like any other field. So I could just put a few terms in the name_suggest = statement above in my code which will match the corresponding field, when searched. But my question is how to do that with a ton of records? I was guessing there would be a standard way for ES to automatically come up with a few terms that could be used as suggestions. For example: using each word in the phrase as a term. I could come up something like that on my own (by breaking each phrase into words) but it seems counter-intuitive to do that on my own since I'd guess there would already be a default way that the user could further tweak if needed. But couldn't find anything like that on SO/blogs/ES docs/elasticsearch-dsl docs after searching for quite sometime. (This post by Adam Wattis was very helpful in getting me started though). Will appreciate any pointers.
I think I figured it out (..phew)
In the indexing function, I need to use the following to enable to the prefix completion suggester:
name_suggest = self.name
instead of:
name_suggest = {'input': something.here }
which seems to be used for more custom cases.
Thanks to this video that helped!

calling a function to obtain a model field value

I'm trying to get a unique value for a field (unique within the db column).
my code (other model fields omitted):
class PlatformUserChildren(models.Model):
dashboard = models.CharField('dashboard URL', max_length=64, unique=True, default=createDashboardCode(self))
def createDashboardCode(self):
stringCheck = False
while stringCheck is False:
newString = str(uuid.uuid4())[:32]
doesStringExist = newString in self.dashboard
if not doesStringExist:
stringCheck = True
return newString
I'm getting name 'self' is not defined as an error.
What should I be passing to the function so that I can check the db column to ensure the value is unique -or- is there a built-in way of doing this?
What I've already tried or looked at:
setting unique=True for the field and using default=uuid.uuid4 - that gives me duplicate values and generates a validation error (SO link)
I'm aware of Django 1.8's UUID field, however i'm on 1.7
The problem lies in the following line (indented for better readability) as you already know and mentioned before:
dashboard = models.CharField(
'dashboard URL',
max_length=64,
unique=True,
default=createDashboardCode(self)
)
In this part:
default=createDashboardCode(self)
you're calling the method createDashboardCode with the argument self. This is wrong, because you never pass self to a method as it is passed by Python. Whenever you call the method createDashboardCode you should do it this way:
createDashboardCode()
That's it, you're not passing the argument self explicitly.
You're getting an error "name 'self' is not defined" because self is not defined. There is no variable self in your code that you can pass to the method.
Now we're one step further, but your problem won't be solved if you just apply this slight change to your code.
The return value from the method createDashboardCode will be assigned to default. That's not what you really want. You have to assign a reference of the method to default:
default = createDashboardCode
Pay attention to the missing brackets. This will call the method every time a new instance of the model is created
Define a function:
def my_function():
print "hello"
and run it in the Python interpreter:
# once like this
my_function()
# and again like this
my_function
and you'll see the difference. That should help you to better comprehend this issue.

Django default=timezone.now + delta

Trying to set a timestamp for a key expiration in Django model and bumped into this issue :
My current code :
key_expires = models.DateTimeField(default=timezone.now() + timezone.timedelta(days=1))
The code above works, however when "timezone.now()" is used, it gets the timestamp form the time when Apache was restarted, so this doesn't work. I did some research and found the solution for that part of the issue, so by replacing "timezone.now()" with "timezone.now", I'm getting the current time stamp every time the object is created, which is perfect, issue is partially solved.
I'm having trouble changing the date by using the "timezone.timedelta(days=1)".
key_expires = models.DateTimeField(default=timezone.now + timezone.timedelta(days=1))
Error I'm getting is :
key_expires = models.DateTimeField(default=timezone.now + timezone.timedelta(days=1))
TypeError: unsupported operand type(s) for +: 'function' and 'datetime.timedelta'
The goal is to set the time stamp 24 hours ahead.
Any help is greatly appreciated.
default takes a callable, so you just need to write a function to do what you want and then provide that as the argument:
def one_day_hence():
return timezone.now() + timezone.timedelta(days=1)
class MyModel(models.Model):
...
key_expires = models.DateTimeField(default=one_day_hence)
(As discussed here, resist the temptation to make this a lambda.)

email__iexact on django doesn't work on postgresql?

Calling UserModel.objects.filter(email__iexact=email) results in the following query
SELECT * FROM "accounts_person" WHERE "accounts_person"."email" = UPPER('my-email#mail.com')
This doesn't find anything because it there's no EMAIL#MAIL.COM in the database, only email#mail.com. Shouldn't the query have been translated to
WHERE UPPER("accounts_person"."email") = UPPER('my-email#mail.com')?
Summary:
UserModel.objects.filter(email=email) # works
UserModel.objects.filter(email__exact=email) # works
UserModel.objects.filter(email__iexact=email) # doesn't work
Clash you ae right this i also faced the same situtaion with postgres sql .
If you go through This ticket
You will get some idea .
Perhaps an option could be passed to EmailField to state whether you want it to lower all case or not. It would save having to do something in the form validation like.
def clean_email(self):
return self.cleaned_data['email'].lower()
My bad. I had patched lookup_cast to be able to use the unaccent module on postgresql and ended up not calling the original lookup_cast afterwards. The generated query now looks like this WHERE UPPER("accounts_person"."email"::text) = UPPER('my-email#mail.com'). This is the default behavior on django.

Some troubles while converting authors id to User model

My Post model have list of authors id
class Post(Document):
authors_id = ListField(IntField(required=True), required=True)
But sometime I need to use default Django User class. How most rapidly I can do it?
(I'm using sqlite for users and sessions and MongoDB (mongoengine ODM) for other. Don't ask why:))
I was tried to write it:
def get_authors(self):
authors = list()
for i in self.authors_id:
authors.append(get_user(IntField.to_python(self.authors_id[i])))
return authors
...and it raises 'list index out of range' exception. (authors id not empty, really). What I'm doing wrong?
Not sure about the to_python method but since you are looping through the authors_id, there is no need to do
authors.append(get_user(IntField.to_python(self.authors_id[i])))
You should be good with
authors.append(get_user(IntField.to_python(i)))
Instead of
IntField.to_python(self.authors_id[i]))
I think you just need to do:
IntField.to_python(i)
In Python the 'for i in some_list' construction gives you elements of the list, not integer indexes.
You said that you were getting this error:
and unbound method to_python() must be called with IntField instance as first argument (got int instance instead)
I got a similar error from MongoEngine. In my case the problem was that I defined the field like this:
foo_id = IntField
The correct way to define it is:
foo_id = IntField()
When I added the parenthesis, the problem went away.