How to remove all of the data in a table using Django - python

I have two questions:
How do I delete a table in Django?
How do I remove all the data in the table?
This is my code, which is not successful:
Reporter.objects.delete()

Inside a manager:
def delete_everything(self):
Reporter.objects.all().delete()
def drop_table(self):
cursor = connection.cursor()
table_name = self.model._meta.db_table
sql = "DROP TABLE %s;" % (table_name, )
cursor.execute(sql)

As per the latest documentation, the correct method to call would be:
Reporter.objects.all().delete()

If you want to remove all the data from all your tables, you might want to try the command python manage.py flush. This will delete all of the data in your tables, but the tables themselves will still exist.
See more here: https://docs.djangoproject.com/en/1.8/ref/django-admin/

Using shell,
1) For Deleting the table:
python manage.py dbshell
>> DROP TABLE {app_name}_{model_name}
2) For removing all data from table:
python manage.py shell
>> from {app_name}.models import {model_name}
>> {model_name}.objects.all().delete()

Django 1.11 delete all objects from a database table -
Entry.objects.all().delete() ## Entry being Model Name.
Refer the Official Django documentation here as quoted below -
https://docs.djangoproject.com/en/1.11/topics/db/queries/#deleting-objects
Note that delete() is the only QuerySet method that is not exposed on a Manager itself. This is a safety mechanism to prevent you from accidentally requesting Entry.objects.delete(), and deleting all the entries. If you do want to delete all the objects, then you have to explicitly request a complete query set:
I myself tried the code snippet seen below within my somefilename.py
# for deleting model objects
from django.db import connection
def del_model_4(self):
with connection.schema_editor() as schema_editor:
schema_editor.delete_model(model_4)
and within my views.py i have a view that simply renders a html page ...
def data_del_4(request):
obj = calc_2() ##
obj.del_model_4()
return render(request, 'dc_dash/data_del_4.html') ##
it ended deleting all entries from - model == model_4 , but now i get to see a Error screen within Admin console when i try to asceratin that all objects of model_4 have been deleted ...
ProgrammingError at /admin/dc_dash/model_4/
relation "dc_dash_model_4" does not exist
LINE 1: SELECT COUNT(*) AS "__count" FROM "dc_dash_model_4"
Do consider that - if we do not go to the ADMIN Console and try and see objects of the model - which have been already deleted - the Django app works just as intended.
django admin screencapture

Use this syntax to delete the rows also to redirect to the homepage (To avoid page load errors) :
def delete_all(self):
Reporter.objects.all().delete()
return HttpResponseRedirect('/')

You can use the Django-Truncate library to delete all data of a table without destroying the table structure.
Example:
First, install django-turncate using your terminal/command line:
pip install django-truncate
Add "django_truncate" to your INSTALLED_APPS in the settings.py file:
INSTALLED_APPS = [
...
'django_truncate',
]
Use this command in your terminal to delete all data of the table from the app.
python manage.py truncate --apps app_name --models table_name

There are a couple of ways:
To delete it directly:
SomeModel.objects.filter(id=id).delete()
To delete it from an instance:
instance1 = SomeModel.objects.get(id=id)
instance1.delete()
// don't use same name

Actually, I un-register the model (the table data that I want to delete) from the admin.py. Then I migrate.
python manage.py makemigrations
python manage.py migrate
python runserver
Then I register the model in the admin.py and do migration again. :) Now, the table is empty. This might not be a professional answer, but it helped me.

Related

FieldError: Unsupported lookup for CharField or join on the field not permitted on Django

Why do I get this error: FieldError: Unsupported lookup 'unaccent' for CharField or join on the field not permitted?
Info
Language: Python
Platform: Django
Database: PostgreSQL
Code
View:
def search(request):
query = request.GET.get("query")
searched = Book.objects.filter(title__unaccent__icontains=query) # Error here
return render(request, "main/search.html", {
"query": query,
"searched": searched,
})
Expected output
Unaccent the query and search for the unaccented version in the database.
Description
I get the error FieldError: Unsupported lookup 'unaccent' for CharField or join on the field not permitted when I try to query the database using __unaccent while using the advanced search features mentioned in the django docs.
If unaccent or any other PostgreSQL specific lookup, first
Add django.contrib.postgres to your settings.py INSTALLED_APPS.
Activate the lookup's extension on PostgreSQL. For unaccent, click here (under "Usage"), or you could
Perform the extension's migration operation. For unaccent, the extension is called UnaccentExtension. You can do this migration operation by
$ ./manage.py makemigrations --empty your_app_name
Then head over to the migration file created in the migrations folder of your app. If you're not sure which one is the file that was just created, the newest file is usually the one you created. To be extra careful, look into the terminal output that was displayed after you ran the above command. It is going to include the name of the file under the heading Migrations for your_app_name.
Then, in that file, delete the content and paste this:
from django.contrib.postgres.operations import UnaccentExtension
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('<your_app_name>', '<previous_migration_file>'),
]
operations = [
UnaccentExtension()
]
In the above code, replace <your_app_name> with the name of your app, and previous_migration_file with the name of the migration file before this one, not including the .py file extension. To find the previous migration file, look at the number of the migration file you're in (for example, in 0008_auto_20220104_1352, 0008 is the number of the file) and subtract 1 (for example, the previous file of 0008_auto_20220104_1352 is going to start with 0007).
After making the changes, python3 manage.py migrate to migrate your changes. You can now access the unaccent extension.

Error after Altering (Dropping columns and Change many-to-many membership) the SQLite in FLASK migrate

I had a User and channel class in my model.py. They had many-to-many relationships. I decided to remove the many-to-many relationships between User and channel and add the many-to-many relationship between Usertemp class and channel class.
After this, I issue:
flask db migrate
flask db upgrade
However I received the error message that SQLite cannot perform this operation consider batch migration. I looked at other familiar topics such as https://github.com/miguelgrinberg/Flask-Migrate/issues/61#issuecomment-208131722
or Why Flask-migrate cannot upgrade when drop column
I have come across this code in both of the above links:
with app.app_context():
if db.engine.url.drivername == 'sqlite':
migrate.init_app(app, db, render_as_batch=True)
else:
migrate.init_app(app, db)
However maybe there is more do to since I still receive the SQLite error.
More Info: After looking at my version files in migration folder I had none as the name of some tables:
This is the first version of my migration file that I use batch migration to drop the column I guess the None is creating the problems:
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.add_column('enroll', sa.Column('usertemp_id', sa.Integer(), nullable=True))
op.drop_constraint(None, 'enroll', type_='foreignkey')
op.create_foreign_key(None, 'enroll', 'usertemp', ['usertemp_id'], ['id'])
with op.batch_alter_table('enroll') as batch_op:
batch_op.drop_column('user_id')
Then I added the following to my code:
convention = {
"ix": 'ix_%(column_0_label)s',
"uq": "uq_%(table_name)s_%(column_0_name)s",
"ck": "ck_%(table_name)s_%(constraint_name)s",
"fk": "fk_%(table_name)s_%(column_0_name)s_%(referred_table_name)s",
"pk": "pk_%(table_name)s"
}
metadata = MetaData(naming_convention=convention)
db = SQLAlchemy(app, metadata=metadata)
And the run the flask db upgrade but again I receive error:
Following is my updated migration version:
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_constraint(None, 'enroll', type_='foreignkey')
op.create_foreign_key(op.f('fk_enroll_usertemp_id_usertemp'), 'enroll', 'usertemp', ['usertemp_id'], ['id'])
op.drop_column('enroll', 'user_id')
op.create_unique_constraint(op.f('uq_user_name'), 'user', ['name'])
I suspect the solution given for render_as_batch=true can fix the issue since some people have posted it is working for them. However can anyone explain how to add it to my files, where I should add it?
I suspect it should be added env.py file. However then what else I should change after?
Is adding the above code including render_as_batch fix the problem by itself.
I would appreciate it if anyone can help since right now anytime I make a mistake I issuedb.drop_all() and create the tables again, which is very inefficient.

migrations are getting created repeatedly

I have created some models and when I run python manage.py db migrate command it creates migrations file, so that is fine.
python manage.py db upgrade command also creates table in Database.
If I again run the python manage.py db migrate command then it is creating migrations file for those models that I have upgraded recently.
Can you please help me to resolve it.
I had same problem and i've resolved it.
In my case, There is a problem on getting current table names.
(when calling get_table_names function in _autogen_for_tables((alembic/autogenerate/compare.py))
I am using sqlalchemy with the mysql-connector.
mysql-connector return table information as bytearray.
so i've changed temporally the following. (base.py(sqlalchemy/dialects/mysql))
#reflection.cache
def get_table_names(self, connection, schema=None, **kw):
"""Return a Unicode SHOW TABLES from a given schema."""
if schema is not None:
current_schema = schema
else:
current_schema = self.default_schema_name
charset = self._connection_charset
if self.server_version_info < (5, 0, 2):
rp = connection.execute(
"SHOW TABLES FROM %s"
% self.identifier_preparer.quote_identifier(current_schema)
)
return [
row[0] for row in self._compat_fetchall(rp, charset=charset)
]
else:
rp = connection.execute(
"SHOW FULL TABLES FROM %s"
% self.identifier_preparer.quote_identifier(current_schema)
)
return [
row[0]
for row in self._compat_fetchall(rp, charset=charset)
if row[1] == "BASE TABLE"
]
to
#reflection.cache
def get_table_names(self, connection, schema=None, **kw):
"""Return a Unicode SHOW TABLES from a given schema."""
if schema is not None:
current_schema = schema
else:
current_schema = self.default_schema_name
charset = self._connection_charset
if self.server_version_info < (5, 0, 2):
rp = connection.execute(
"SHOW TABLES FROM %s"
% self.identifier_preparer.quote_identifier(current_schema)
)
return [
row[0] for row in self._compat_fetchall(rp, charset=charset)
]
else:
rp = connection.execute(
"SHOW FULL TABLES FROM %s"
% self.identifier_preparer.quote_identifier(current_schema)
)
return [
row[0].decode("utf-8")
for row in self._compat_fetchall(rp, charset=charset)
if row[1].decode("utf-8") == "BASE TABLE"
]
I think the problem is to manage.py. If you did it as described on flask-migration site and stored all your models in this file - flask-migration just get these models and generates migrations and will do it always. You wrapped the standard command in your file and this is the problem.
If you want to fix it - store models in another directory (or another file), add them to an app and use command flask db migrate. In this case, flask-migration will generate migration for models only at first time, for others, it will detect changes and generate migrations only for changes.
But be careful, flask-migration don't see all changes. From site:
The migration script needs to be reviewed and edited, as Alembic currently does not detect every change you make to your models. In particular, Alembic is currently unable to detect table name changes, column name changes, or anonymously named constraints. A detailed summary of limitations can be found in the Alembic autogenerate documentation.

Django raw raises relation does not exist

i am getting a relation does not exist and I cant find a solution.
error:relation "sales_Oeslshstsql" does not exist
LINE 1: SELECT * FROM "sales_Oeslshstsql
(app name is sales)
model:
class Oeslshstsql(models.Model):
hst_prd = models.SmallIntegerField()
hst_year = models.SmallIntegerField()
cus_no = models.CharField(max_length=12)
item_no = models.CharField(max_length=15)
.....
a4glidentity = models.IntegerField(db_column='A4GLIdentity', primary_key = True)
class Meta:
managed = False
db_table = 'OESLSHST_SQL'
def __str__(self):
return (self.hst_year)
View:
def sales(request):
#sales_list = Oeslshstsql.objects.all().order_by('hst_year','hst_prd').reverse()
s = Oeslshstsql.objects.raw('SELECT * FROM "sales_Oeslshstsql"')
sales_list = s
return render(request,'saleslist.html',{'sales_list':sales_list})
The error is raised when s is evaluated. I tried switching cases in the select and messed with migrations no luck.
I am migrating an existing app to Django using a postgres backend, any help would be appreciated.
try:
s = Oeslshstsql.objects.raw('SELECT a4glidentity as id, ... , FROM "OESLSHST_SQL"')
http://docs.djangoproject.com/en/1.8/ref/models/options/#db-table seems your tablename in query is wrong
edit: you should add the primary key as id see https://docs.djangoproject.com/en/1.8/topics/db/sql/#mapping-query-fields-to-model-fields
Hi I had the same issue migrating an existing app to 1.11. The only solution I found was ....
Clear all all files from the app's migrations dir leaving only the init.py file
Make sure that the admin.py file is empty
Run manage.py makemigrations
Run manage.py sqlmigrate <app_label> 0001
copy the sql output
using pgAdminIII select "Execute arbitrary SQL queries"
Paste and execute the SQL statements in pgAdminIII
This was the only solution I could find, bit of a hack true, but worked. Hope it helps.This would also work via psql terminal I suppose, but I used pgAdmin

Getting missing column error whenever a model is saved

I'm having trouble creating a model in django. I wrote a model like this:
from django.db import models
class FooModel(models.Model):
name = models.CharField(max_length = 255)
I run
manage.py syncdb
But when I'm in the shell, I can't save an instance. Every time I call it, it tells me it's missing a column
manage.py shell
>>from app.models import FooModel
>>foo = FooModel()
>>foo.name = 'foo'
>>foo.save()
DatabaseError: column "name" of relation "ecommerce_foomodel" does not exist
LINE 1: INSERT INTO "ecommerce_foomodel" ("name") VALUES (E'123123as...
We're using postgres.
The database table was created before you added the corresponding fields.
So, you can recreate all of the tables of that app (in case you don't have any useful data) using:
python manage.py reset ecommerce
Or, you should migrate the database to the latest version using South.

Categories

Resources