django db_table not changing - python

Hi i have created a dynamic table model and it is not changing whenever i select another table from the list i ve created in the template...
if i select the first table to see the data of it and then go back to the list and select the second table i get this error
InternalError: (1054, u"Unknown column 'table_name1.id' in 'field list'")
So, when I change from http://127.0.0.1:8000/tables/id=1 to http://127.0.0.1:8000/tables/id=2 it gives me the error.
But if I restart the server and go straight to
http://127.0.0.1:8000/tables/id=2 it works.
But now http://127.0.0.1:8000/tables/id=1 it doesn't work.
this is the party of views.py
def addview(request, pk):
table_name = Crawledtables.objects.get(id=pk)
print table_name
AllTables._meta.db_table = table_name.name
print AllTables._meta.db_table
tbl_detail = AllTables.objects.all()
print tbl_detail
return render(request, 'tables/table_list.html', {'details': tbl_detail})
The prints are just for testing and they work.
It prints out the correct table I select.
But the tbl_detail it doesn't print when i go to the 2nd table that i selected. only when I restart the server.
CrawledTables holds all the table names inside that DB with the date of creation of each table. I get the table name of the CrawledTables and put it in the AllTables._meta.db_table.
AllTables access all the tables in the DB ( same structure, different table names with different data inside of each table)
Do I need to clear the db_table everytime i go to the selected table?
I have done all the migrations correctly.
Please help me. Thank you in advance.

Related

Delete rows in database where id not found in a filtered query in Flask-SQLAlchemy

I'd like to remove rows in my database where an ID is not found amongst ID's from a filtered query. In layman's term, "Delete rows if the row's ID is not amongst ID's sourced from a query that kept unique users by name".
For example, I would like to clear out users in my database with duplicate names:
from project.models import db, User
query = User.query.group_by(User.name)
User.query.filter(** filter out User.id not found in `query.id` **).delete()
db.session.commit()
You can call filter multiple times on same query object.
query = User.query.group_by(User.name).all()
delete_query = db.session.query()
for user in query:
delete_query = delete_query.filter(User.id != user.id)
delete_query.delete()
db.session.commit()
Ok I came to a working solution after some digging:
from project.models import db, User
# Filter id's where `User.name` is unique
query = User.query.with_entities(User.id).group_by(User.name)
del_query = User.__table__.delete().where(User.id.not_in(query))
# Delete posts with non-unique names
db.session.execute(del_query)
db.session.commit()
This solution does not filter iteratively using User.query.group_by(User.name).all(), which could introduce time and memory issues if your database is large.

How to input query flat output from a different database to a Django model?

First off, I'm new to Django and Python overall - so I might be asking the wrong question, if that's the case please tell me where to look. If not, continue:
I'm working with a Django app that queries a Wordpress database, I have created a connection and wrote the query as such:
cnxn = MySQLdb.connect(host="localhost",
user="root",
passwd="password",
db="dbname")
query = '''SELECT post_id, label, meta_value
FROM table'''
The issue is that the data is in a flat format, where the label should be the column name and the meta_value should be the value, output:
post_id label meta_value
------- ----- ----------
1 name example name
1 email example#mail.com
2 name example name 2
2 email example2#mail.com
Please keep in mind that there are 24 labels for each post_id, so each form entry adds 24 lines to the table.
How would I got about creating a Django model that appends the data from the table in a pivot style format to be able to display it nicely in the template? The goal is to be able to display 4-5 labels on the main page with a link on each line that shows the full input on a separate page.
Desired output:
post_id name email
------- ---- -----
1 example name example#mail.com
2 example name 2 example2#mail.com
Thank you!
I ended up converting the initial output to a pivot using Pandas, then uploaded the df into a new model that I created with the same exact field names.
def users(request):
df = pd.read_sql(query, cnxn)
pvt = df.pivot(columns='label', index='post_id', values='meta_value')
pvt = pvt.reset_index()
pvt['id'] = pvt.index
pvt.to_sql('table_name', cnxn, if_exists='replace')
The model configured includes all the fields in the queried table, so the outcome matched the model and I can treat it as an object.
Hope this helps.

Look for existing entry in database before trying to insert

I am currently working with Access 2013. I have built a database that revolves around applicants submitting for a Job. The database is set up so that a person can apply for many different jobs, when the same person applies for a job through our website (uses JotForms) it automatically updates the database.
I have a Python script pulling the applicants submission information from an email which updates the database. The problem that I am running into is that within the database I have the applicants primary email set to "no duplicates", thus not allowing the same person to apply for many different jobs as the Python script is trying to create a new record within the database causing an error.
Within my Access form (VBA) or in Python what do I need to write to tell my database if the primary emails are the same only create a new record within the position applied for table that is related to the persons primary email?
Tables:
tblPerson_Information tblPosition_Applied_for
Personal_ID (PK) Position_ID
First_Name Position_Personal_ID (FK)
Last_Name Date_of_Submission
Clearance_Type
Primary_Phone
Primary_email
Education_Level
Simply look up the email address in the [tblPerson_Information] table:
primary_email = 'gord#example.com' # test data
crsr = conn.cursor()
sql = """\
SELECT Personal_ID FROM tblPerson_Information WHERE Primary_email=?
"""
crsr.execute(sql, (primary_email))
row = crsr.fetchone()
if row is not None:
personal_id = row[0]
print('Email found: tblPerson_Information.Personal_ID = {0}'.format(personal_id))
else:
print('Email not found in tblPerson_Information')

Does PeeWee support interaction with MySQL Views?

I am trying to access pre-created MySQL View in the database via. peewee treating it as a table [peewee.model], however I am still prompted with Operational Error 1054 unknown column.
Does PeeWee Supports interactions with database view ?
Peewee has been able to query against views when I've tried it, but while typing up a simple proof-of-concept I ran into two potential gotcha's.
First, the code:
from peewee import *
db = SqliteDatabase(':memory:')
class Foo(Model):
name = TextField()
class Meta: database = db
db.create_tables([Foo])
for name in ('huey', 'mickey', 'zaizee'):
Foo.create(name=name)
OK -- nothing exciting, just loaded three names into a table. Then I made a view that corresponds to the upper-case conversion of the name:
db.execute_sql('CREATE VIEW foo_view AS SELECT UPPER(name) FROM foo')
I then tried the following, which failed:
class FooView(Foo):
class Meta:
db_table = 'foo_view'
print [fv.name for fv in FooView.select()]
Then I ran into the first issue.
When I subclassed "Foo", I brought along a primary key column named "id". Since I used a bare select() (FooView.select()), peewee assumed i wasnted both the "id" and the "name". Since the view has no "id", I got an error.
I tried again, specifying only the name:
print [fv.name for fv in FooView.select(FooView.name)]
This also failed.
The reason this second query fails can be found by looking at the cursor description on a bare select:
curs = db.execute_sql('select * from foo_view')
print curs.description[0][0] # Print the first column's name.
# prints UPPER(name)
SQLite named the view's column "UPPER(name)". To fix this, I redefined the view:
db.execute_sql('CREATE VIEW foo_view AS SELECT UPPER(name) AS name FROM foo')
Now, when I query the view it works just fine:
print [x.name for x in FooView.select(FooView.name)]
# prints ['HUEY', 'MICKEY', 'ZAIZEE']
Hope that helps.

Querying a for table not defined in models Sqlalchemy

I have this table in my database (using Posgresql and Sqlalchemy) called "participants".
In my models.py I want to access the participant's records. Since participants is not in my models.py and resides as a table in my db, when I do query = db.session.query('participants').order_by('name').all() I get an error:
ProgrammingError: (ProgrammingError) column "participants" does not exist
LINE 1: SELECT participants ORDER BY name
^
What can I do to retrieve this information?
Like the comment on the original post, did something like:
query = db.engine.execute('select * from participants order by name')
This doesn't give it in the same format as the query so I could use it for what I needed (a dictionary output, and a format for web), so I did:
partner_list = []
for record in query:
record_dict = dict(record)
partner_list.append(record_dict)

Categories