I want to view Django default User table from MySQL console.
I know to access from django shell or python by simply importing it.
from django.contrib.auth.models import User
is there a way to view it from MySQL console itself?
and where it will be located? i mean in which database django user table belongs?
Of course you can see it in the database.
As with all other models, unless instructed otherwise by using a db_table property in the Meta class, Django uses the naming schema appname_modelname for tables - so in this case the table is auth_user.
If you don't know and can't find the source, you can ask the model itself - ie User._meta.db_table.
The tables are located in the database which you have specified in settings.py. The django user table will be located at yourdbname.auth_user. All user defined models will be stored as yourdbname.appname_modelname
You can use "python manage.py dbshell" (in order to do this in linux, you need to install mysqlclient) and use "show tables;" and "select * from yourdb.table"
Related
I am new to Django.
I created the sqlite3 database called "test.db" and I'm not using the models.py or default database to sync in views.py for saving data.
I've created the HTML to get the value to store in test.db and print the data using the normal sqlite3 query.
The problem is that I have to access the test.db database in admin panel to search and print the data stored in the database. How to do this task?
You'll need to set up models.py for you to see data in admin functionality of the site. This is because the register statement register(*models, site=django.admin.sites.site) uses the models to display the data. If you are working on legacy database which you aren't, in this case, try this
I am creating a Django web application and I am using Postgres for my database.
Under my project, I have a web application named 'home', and I created a table named 'myTable' in Postgres.
Whenever I try to save something in the table, Django automatically looks for the table called 'home_myTable' instead of 'myTable'. For example, if I do
python manage.py migrate
I get the following error:
django.db.utils.ProgrammingError: relation "home_myTable" does not exist
I have been working around this by manually giving Postgres commands using Psycopg2, but this is really annoying.
Is there a way to stop Django from automatically start looking for 'home_myTable' and instead make it search the table that I want?
You can set the db_table Meta option for you model.
class MyTable(models.Model):
...
class Meta:
db_table = 'mytable'
Unless you are dealing with a legacy database, I recommend that you define your models, create migrations, then let Django create the tables, instead of creating the tables manually as you are doing.
I am using Microsoft SQL server with Django (1.8.4). When I run migrations, the tables get created as
domain\username.table_name1
domain\username.table_name2
instead of
table_name1
table_name2
How do I resolve this? Is there a configuration for the naming format?
Django is running on Ubuntu 14.04.
This is by design, so you could have similar models in multiple applications. Otherwise, two application might use an "Expenses" or "Songs" model, and the DB will fail to create two tables with the same name.
You should usually stick with the default naming scheme.
The db_table Meta option does let you specify the table name, but it is mostly for existing legacy database.
Alter user's default schema you are connecting with. When you create a user and allow it to access a database, server create a schema of the same name and sets it as user's default schema. If you did not set one explicitly.Django does not work with schemas and does not try to create "dbo.MyTable" - it attempts to create "MyTable" and finally it goes to the default schema which is your username.
Scenario
I have a basic Django app in which users (django's authentication built in model) and reports have a many-to-many relationship.
The Problem
Django does not create a corresponding table to handle this relationship. My application is called reports. There is an error in the admin system upon trying to create a report and assign users to it. It tries to query the table reports_report_users and it fails as it does not exist.
models.py code
from django.db import models
from django.contrib.auth.models import User
class Report(models.Model):
name = models.CharField(max_length=30, blank=False)
users = models.ManyToManyField(User, related_name='reports')
def __unicode__(self):
return self.name
Attempted Solutions
Used this link as a reference: https://docs.djangoproject.com/en/1.4/topics/db/examples/many_to_many/
Ran manage.py syncdb about 300 times - ok, only once, but there were no errors and upon inspecting the SQLite db there were no additional tables created :(
It seems like you've added to the Report model after the first sync. Thus you're dealing with a migration, which django doesn't do natively.
First, Inspect the sql output, make sure that the create table instruction for your many to many relationship is there.
python manage.py sqlall
Assuming the problem is that this is a migration, which django doesn't handle natively, you've got three options:
1) Delete all db tables for this app, then run syncdb again.
2) Manually create the tables (fairly easy to copy paste the create sql from the sqlall command)
3) Start using a migration framework like South.
In the long run you'll appreciate the investment in learning south. In the short term, deleting the DB file is the fastest.-
Have you deleted your db file and run manage.py syncdb again?
The import statement import the needed parts. but is the "user" class already made when you put that into your installed apps? or do you still need to clarify in models.py in order to make the table in the db? or can someone expand on how to use django users and sessions? I'm looking over the django docs right now and they all just go over how to use the thing once. they never put the code in a syntax where users are going to be the ones using the code through a browser and not you through a python shell.
All installed apps can contribute to the database schema. django.contrib.auth.models contributes, among others, the auth_user table behind the django.contrib.auth.models.User model, therefore you do not have to worry about recreating it unless you have a specific reason to do so.
There's a number of things going on here. As you're aware, Django comes with a number of "contrib" packages that can be used in your app. You "activate" these by putting them into your INSTALLED_APPS.
When you run python manage.py syncdb, Django parse the models.py files of every app in INSTALLED_APPS and creates the associated tables in your database. So, once you have added django.contrib.auth to your INSTALLED_APPS and ran syncdb, the tables for User and Group are there and ready to be used.
Now, if you want to use these models in your other apps, you can import them, as you mention, with something like from django.contrib.auth.models import User. You can then do something like create a ForeignKey, OneToOneField or ManyToManyField on one of your models to the User model. When you do this, no tables are created (with the exception of ManyToManyField; more on that in a bit). The same table is always used for User, just as for any of your own models that you might create relationships between.
ManyToManyFields are slightly different in that an intermediary table is created (often called a "join table") that links both sides of the relationship together. However, this is purely for the purposes of that one particular relationship -- nothing about the actual User table is different or changed in any way.
The point is that one table is created for User and this same table is used to store all Users no matter what context they were created in. You can import User into any and all of your apps, create as many and as varied relationships as you like and nothing really changes as far as User is concerned.
If the table name or something else does not fit in your needs you can always just extend the User model.
from django.contrib.auth.models import User
class Employee(User):
...
Any class extending Model class in models.py contributes to database schema. That means, django search your (and also django core) model.py files and looks for any class that extends Model like:
some models.py
class SomeModel(Model):
...
...
class Otherthing(Model):
...
that is also applies for django core code files. Since all Database tables named using application label and model name, database ables created by django also have that...
For example,
from django.contrib.auth.models import User
If you track file hierarchy django -> contrib -> auth and open models.py file, you will see related model. Ther are also other Model classes in here, like Permission and Group models.
Since these models are under auth application, database tables are auth_user, auth_perission and auth_group
When you run manage.py syncdb command for the first time, django will create these tables...