Object could not be found in database for SearchResult django haystack - django

I am using haystack with elasticsearch. I have build index data using rebuild_index command. But when I tried to search for object, its giving me following error:
"Object could not be found in database for SearchResult ' (pk=u'118')>'."
I have double checked in database, no records were deleted. But I am still getting this error.
Can anyone please help with it?
Thanks.

It looks like you are searching for your pk as a string and not an integer, not sure what your query looks like.
as integer, notice no quotes when defining pk_int:
pk_int = 118
Model.objects.get(pk = pk_int)
if your variable containing the pk is a string already, use int():
pk_string = '118'
Model.objects.get(pk = int(pk_string))

Answering it to mark it resolved.
It was database config issue. I am having different settings file for different environment, and was having mismatch for database config in both files.

For anyone coming from a web search engine, if the accepted answer doesn't work for you, you can also try rebuilding the index:
./manage.py rebuild_index
I had the same error message as the OP, and rebuilding the index solved my issue.

Related

How to save an array of text in PostgreSQL using Django model.?

I am trying to save an array of text containing category types for a hotel system which looks something like this ['category-1', category-2', category-3, category-4] . I am using category_type = ArrayField(models.CharField(max_length=200),null=True) in my models.py
The error i get is
malformed array literal: "" LINE 1: ..., '{category-1,
category-2}'::varchar(200)[], ''::varcha...
^ DETAIL: Array value must start with "{" or dimension information.
The error persist even after processing python list from ['category-1', category-2', category-3, category-4] to {category-1, category-2, category-3, category-4}.
I have gone through postgresql documentation and have found very limited help,
https://pganalyze.com/docs/log-insights/app-errors/U114 this is something similar posted to what i am facing problem with.
Could someone please tell me what am i doing wrong? Any help would be appreciated.
EDIT:
Following is in my View.py
hotel_category=categoryTable(category_type=categorytype)
hotel_category.save()
and i am using categorytype=request.POST.getlist('category-type') in my Views.py to get it from the POST request after user submits the form. This returns a Python list that i have mentioned above, i have manipulated this list to match PostgreSQL ArrayField with '{','}' but i still have this error. If there is anything else you would like me to add, please let me know. :)
This is an update/answer to my question for anyone who faces this issue in the future. After struggling to find help from different resources, i decided to use JSON string to store my python list.
I am using :
categorytype = json.dumps(request.POST.getlist('category-type'))
to encode and using JSONDecoder() to fetch from database and decode. I have no idea how would this impact my further development but for now it seems a decent approach since personally i think ArrayFields are not well supported and documented in Django.
I will keep this post updated as i progress further on how this approach has impacted my development.
Have a nice day.

How to get Django Report Builder working properly?

Documentation provided by Burke isn't very thorough and there isn't a lot of information on Django Report Builder yet, not even youtube videos. Does anybody have any information on how to get work Report Builder Working in Django? I currently keep getting this error message:
(1146, "Table 'epic_test2.report_builder_report' doesn't exist")
I've installed it, but I can't create reports yet and the documentation I do have isn't very helpful. Any advice would be great! Thanks!
UPDATE TO POST/ 5.12.15 -
I fixed the first issue, but now I have a new error. :) Yay. It's a Field Error
"Cannot Resolve keyword 'name' into field. Choices are: app_label, id, logentry, model, permission, report.
ANY clues for this one would be helpful... going into third hour of troubleshooting. :)
Thanks!
~Heather
Sounds like you may need to run a migrate to create the Report Builder tables required.
python manage.py migrate
I would suggest you do a full search in your code for "name", something like: grep -R "name" ., and see where it's defined, is it a valid field in models.py, etc.
I've had similar error before, turned out, I updated my variable name from something like "name" to "new_name" in my model. The way report-builder works, it saves column (field) name in the database. If it's an old field name, there will be error. Luckily this is easy to fix: go to report-builder UI, remove the row for that column, then add the column back, then the new field name would be saved.
Hope this helps.

Django and MongoDB issues with Django admin

I'm trying to integrate MongoDB with Django's Admin system (as per the Tumblelog tutorial on the mongodb site and hitting a bug when I try to python manage.py syncdb - it gives me this error - and I'm not entirely sure what to do about it. Thanks!
pymongo.errors.OperationFailure: command SON([('create', u'auth_permission'), ('max', False), ('capped', False), ('size', 0.0)]) failed: exception: create collection invalid size spec
```
Seems there is a problem with permission app, Try to add djangotoolbox to you settings.py.
The nature of NoSQL is not compatible with django ORM neither with auth, permission apps which they depends on JOINS to working together.
Anyway, Have you met django-nonrel ? django-nonrel provides more apps and solutions to work with MongoDB and other NoSQL datastores.
See this link: https://github.com/django-nonrel/mongodb-engine/pull/134
The main issue is that for collections that have an uncapped size, set a large capped size instead of so that the subsequent command does not fail.
I'm assuming you're on django-nonrel 1.3.1. I forked off of django-nonrel/mongodb-engine and have the fix in https://github.com/statguyjames/mongodb-engine.git#master.
First I tried statguy's solution by replacing sql_create_model() method entirely - but it didn't work, some errors kept appearing.
Then what I did was that I added
size = getattr(model._meta, 'collection_size', None)
if size is not None:
kwargs['size'] = size
else:
kwargs['size'] = 10000000000
right after
for option, mongo_option in [
('capped', 'capped'),
('collection_size', 'size'),
('collection_max', 'max')
]:
kwargs[mongo_option] = getattr(model._meta, option, False)
in file creation.py from django_mongodb_engine.
This way the error wasn't appearing anymore. However, due to that immense value, mongo will allocate some files on disk that have basically turned my current database from a few MB to about 17GB - ending in an error to allocate new file.
So careful about enforcing a value. I might be missing something, though.

DatabaseError: value too long for type character varying(100)

I have a Django web site running a mini CMS we've built internally years ago, it's using postgresql. When saving a simple title and a paragraph of text I get the following error:
value too long for type character varying(100)
The weird thing is, not a single column is varying(100) they are all 200 or 250, even the default Django ones have been changed from the 100 to 200 due to a re-opened ticket mentioned here
Does anyone know of a solution to this problem?
I can bet money you have a models.SlugField without length set. The default length is 50 characters, most likely it's not enough for your use case.
Change it to models.SlugField(max_length=255) and migrate your database schema.
I also had this problem when using a filefield and was scratching my head for a while. Of course the default FileField instances are created with a 100 character limit.
https://docs.djangoproject.com/en/dev/ref/models/fields/#filefield
This is an error message from Postgres and not django.
You seem to have changed the length of the field in the models.py, but that doesn't change the database length which was created when you did a manage.py syncdb.
You have to alter the length of the field in the database, directly.
Django 2.1
I encountered this problem while switching from sqlite3 to postgresql.
Remove the migration files in each app's migrations folder except __init__.py
Then re-run migration
(venv)myapp$python manage.py makemigrations
(venv)myapp$python manage.py migrate
(venv)myapp$python manage.py runserver
I had a similar problem with django-autoslugfield
I was using a similar package and then switched over to django-autoslugfield
I was getting this error:
value too long for type character varying(50)
despite the fact that my models.py had:
slug = AutoSlugField(max_length=255, populate_from='name', unique=True)
and in my db the it the type was
character varying 255
once i remove max_length=255 from the field i.e.
slug = AutoSlugField(populate_from='name', unique=True)
then it worked fine
i went through this same error. and when i made changes in the modele, i kept having the same error.
Here is how i fixed it.
It might be necessary to skip few migrations for the program to only use the migration where changes have been made for the CharField max_lenght.
for that you have to run
python manage.py showmigrations
to see which migrations have not been made.
then you skip them until you get to the last with the command
python manage.py migrate <app> 000_migration_number --fake
I realize the question is already answered but for others that come here when looking for the error message:
In my case the problem was that my table name exceeded 50 characters. Apparently this is not allowed. Changing the table name solved the problem.
Read more here: https://code.djangoproject.com/ticket/18959
Michael Samoylov's answer pointed me in the right direction. I had the same error come up, except it was with a FileField.
Fields have a max_length, even if you have not explicitly set a max_length. Increase the value so that your data fits to avoid the error.
In my case, the data was too large to reasonably store in the database. I resorted to saving my file to disk, then saving the file path in the database.
I had this problem when I wanted to set max_length to a lower value on a FileField.
Interestingly, the error message states value too long but in my case the value was too short.
The solution was to set back the max_length to its old value. It's quite weird that the migration couldn't be done.
I also had to delete the wrongly generated migrations files and rerun python manage.py makemigrations and python manage.py migrate.
If it's not SlugField, FileField, or any other field mentioned here--scroll back to where the migration got stuck in the terminal. For me it was AddField
Good talk.
First, try setting max_length to something reasonable on all applicable field types in your model.
For example: MyText = models.CharField(max_length=2000)
If you don't set a max_length, your database might be applying a default max_length, shorter than the length of your input data, causing your value too long for type character error.
If that doesn't work and you started in SQLite and changed databases to PostgreSQL, the previous migrations from SQLite might be interfering with the new PostgreSQL migrations.
Go into the migrations folder for your project and delete the old migration files to get a fresh start. Then try makemigrations and migrate again :)
predefined fields in model.py creates the problem. Extend it to desired length, i think problem will be resolved.
For the FileField and the ImageField, from the Django docs:
FileField instances are created in your database as varchar columns with a default max length of 100 characters. As with other fields, you can change the maximum length using the max_length argument.
The value can be 100 or 25 or 17 whatever , the reason behind that is models , search where you have added that particular length(17,25) and you will get the bug ! ,
You are trying to impose a field length more than mentioned one.
This solved my issue , I hope it will help you too

django database sync problem

In my web django, I do a query of Models.objects.all() or actually any other models in the views.py. It returns me a [,] value (empty array) - view from logs. However back in the manage.py shell, it is able to retrieve the objects. In mysql database, the data entries are there!
I have no idea how to debug from here, is there a way to resolve this error? This is not my first time. I have been doing django project for several months already. Only today, it is giving me this problem.
EDIT : This problem is very weird, I'm just curious whether anybody has encountered this problem before, I'm using Eclipse to edit the scripts(views.py, models.py etc).
Do you have __repr__ defined for your objects? Take a look at the HTML source generated by Django -- there's a good chance it looks like:
[<Model: model object>, <Model: model object>, ...]
Your web browser then interprets the <...> as an HTML tag that it doesn't understand, and thus can't render, and displays only the content outside the tags (the "[", ",", and "]" characters).