I inserted through SQL (manage.py dbshell) a handful of records. I then went to view the records in the Django admin interface. All of the records appeared, as expected.
However, any queries via Django (not direct SQL), did not return the records. Only after I went to a record through the admin interface, and clicked Save did the record appear in the query.
I'm new to Django, but after repeated searching and reading Django documentation, I did not find an answer. Any idea what is going on, and what can be done to make the SQL INSERT statements appear in queries without visiting each record to Save it through the admin interface?
Maybe you have to declare the transaction "dirty":
from django.db import transaction
transaction.set_dirty()
Related
Django Admin change "history" logs
As you know Django Admin's sidebar shows a list of APP > MODELS. When you click on a MODEL, the focus area displays a list of RECORDS. Click on a RECORD, then the focus area shows a single RECORD as a form which you can change.
Here, in the upper right hand corner of the change form, is a gray pill-shaped button labeled "HISTORY" with entries like:
Dec. 9, 2021, 4:38 p.m. me Changed Table price headers.
Feb. 26, 2022, 2:06 p.m. me Changed Table date headers.
What I need is a copy of the record data before each change. Later, I need to query the Django database and--in this example--get the version of the record prior to the Feb 26 change.
How can Django Admin change "history" logs be used to also store copy of data?
Not for every Django Model
The rest of the Models & Records in Django Admin should behave normally. I only have a few Models where I need history log and data.
Not to be confused with model history
When discussing this with other Django developers I have to explain the history I need is for the record's data, and not the model. There is a third-party Django packages which does the other thing which I don't need.
Why do I need this?
In my Django site I run a script which requires settings. I'm querying a non-Django-related MongoDB database where I keep a copy of reports generated by several third-party vendors. To help me manage and utilize this data I need a record of each report's headers or schema.
There is a tight coupling between the data I'm storing in Mongo and the data's schema. For this reason I'm storing these headers as "settings" in Django's database. It's been really convient for me to use the Django Administration page to update these schema settings.
However, once in a blue moon a third-party will rename a header or add a new column to their report. When this happens I have to update the settings record in the Django Admin. Currently I don't have access to the historical schemas in my Django project, and have to write custom scripts to access historical data.
My wish is store each version of these records, so I can check the settings change history log. When the requested records needs a historical schema, I can then query the Django database to recall the valid headers for the date range.
Have you tried django-simple-history? Works fine for me.
We are migrating our intranet application and have decided to choose the Django framework. We control all of our database via source control and managed scripts, so we do not use the migrate feature to create tables for us. Views and Tables can change, all business logic is held in the database.
I want to make Django API endpoint which is basically select * from my_table_or_view; and Django can return a JSON response with the column names and values. Some database tables have close to 100 columns each so I don't want to write out each and every field name and type just to return a query. What happens if we add another column to the view - will I have to update it in Django as well? What if I change the column type - will my application fail as well?
The frontend is written in VueJS - it makes a request to the API endpoint and should use the columns selected in the frontend, obviously if I remove a column it will break but I don't want to have to add the column in the django framework even if it is not used.
I've read the raw SQL queries section of the docs but i'm not sure where this applies to. Does this logic sit in the views section?
I've tried directing a URL endpoint in URLS.py to a custom class in views.py but not sure this is correct, does this logic need to be in a serializer?
I'd like the simplest method possible, potentially not using models, just raw SQL is fine.
One option for you is to sync your db to django models every time you change your schema and then use it normally, both ORM and raw SQL queries would be possible.
There's a function called inspectdb that does that natively, here is a reference to it
https://docs.djangoproject.com/en/3.1/ref/django-admin/#inspectdb
I am having a hard time trying to come up with a reasonable design for my project. I have an existing Postgres database that gets constantly updated from other Python scripts.
The web server built on Django framework will access the Postgres database to update User models only and display blog information for the logged in Users. The blog information is what is being updated overnight by other Python scripts.
Now my question, if I have to syncdb my blog model with existing Postgres database, would that cause any problem?
ex:
models.py
class Blog:
title=...
content=...
author=....
And say my Postgres db called mydb has many tables, one of which is blog table and contains columns for title, content and author.
How would make my model in sync with existing database?
Now lets say I included a new column in my db which is date of entry.
If I simply update my model to :
class Blog:
title=...
content=...
author=....
date of entry=...
will it work.
what are the potential problems here and any simpler solutions for these?
P.S: I have used South in the past. but the situation here is different. I am using db that is read-only from Django's point of view, and no data migration is necessary as of now.
If your database is read-only, you don't have to do syncdb. Use managed=False and the db_table meta option on your model to specify the table name it corresponds to, and likewise for the field column names.
If you haven't already, see the doc on legacy databases for more info.
I have a model Sessions that contains almost 10,000 objects. Each session has a foreign key relationship to a Subject. The problem is that when loading a page that has a foreign key field to a session, the page takes a long time to load.
To fix this, I like to have an admin on which you select a subject from one ForeignKey field, and have the sessions that have a relationship with that subject appear in a second ForeignKey field.
Is this something that is possible from within the django admin interface? I have checked out django-smart-selects (though I'm unsure if that would actually do what I'd like it to), but when I try to import it I get an error "No module named admin_static", which I believe may be because I'm using an outdated version of Django (v 1.1).
Any help would be appreciated. Thanks.
Are you sure you don't just need raw_id_fields?
https://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.raw_id_fields
When you visit an admin change form that has a foreign key to Sessions it will build a select dropdown with 10,000 entries. This is probably your slowdown.
I'm using Django 1.4 and experiencing an unusual problem with hidden rows in my admin site.
I can view individual models in my app, and they appear to be querying the database correctly when I click into each one; I can see the number of matches in the top left corner ("3 locations" below my actions bar, for example). However, entries do not appear below that count.
If I try to open an entry directly using the URL, I also get a blank page.
I haven't customized the templates or css. Anyone have experience with this type of problem?
Update: if foreign keys are removed in the model, the entries reappear below the counts. However, that FK field is needed so still trying to identify the root cause.
Fixed. This appears to happen when the model uses a ForeignKey field, and the table it points to does not have its data loaded. That accounts for the model seeing the number of rows, but not being able to pull up the individual entries.
I had this same issue (i.e. I could see the model count in the admin, but not the actual records).
I have a model (Category) with a ForeignKey to another model (Competition). I looked in the database and somewhere along the line while doing some code refactoring on the models and migrations, I ended up with the FKs on the Categories pointing to a non-existent Competition.
When I updated these FKs in the database, the records reappeared in the admin.
In admin.py where you use foreign key you have to add at the last _id for example my foreign key I'd is iClientId so in admin.py we have to write in list_display iClientId_id.