I am experimenting with a new blog database design and there are some tests I would like to run in web2py's administrative interface.
I started by creating a new web2py application called newblog from web2py's admin interface.
Next, I created newblog/models/appdb.py, below
Then I surfed to the admin interface at https://172.25.1.1/newblog/appadmin/index to ensure the database was created
I checked the filesystem and databases/newblog.db has a brand new creation time
I clicked through the appadmin menu to see my new database: "web2py" > "This App" > "Database"
Problem: The problem is I don't see it in the database admin interface for newblog. I have seen other empty web2py databases displayed in the appadmin interface, so I don't understand why mine does not show up there.
Question: Is this expected behavior? If so, what are the minimal steps I need to take for my web2py database to show up in appadmin?
"""
newblog/models/appdb.py
"""
def build_new_table():
return dict({'ugly_dict': 42})
db = DAL('sqlite://newblog.db')
## Build a table of tables, by the type of table (i.e. post, code, etc)
db.define_table('db_type',
Field('name', length=32, notnull=True, unique=True,
comment="Name of the database table"),
#IS_IN_DB(db, 'db.%s.name' % db.db_type.name)),
Field('database_pointer', notnull=True, unique=True,
compute=build_new_table(),
comment="Reference to the database table identified by 'name'",
),
)
## Define tags for the database items
db.define_table('tags',
Field('name', length=32, notnull=True, unique=True),
)
It sounds like you have the default db.py file in addition to your custom appdb.py file. Note, model files are executed in alphabetical order, so db.py is executed after your file. db.py assigns a different database connection to the variable db, so only that database is showing up in appadmin. You should either use the same database for both sets of tables, or use different variables for the two database connection objects. For example, in appdb.py, you might do:
blogdb = DAL('sqlite:\\newblog.db')
If you want to use the same database for all tables, then just define your DAL object in the first file (in this case, appdb.py), and you can refer to it in all subsequent model files (do not redefine it).
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
How does Isolation dependencies between mongdb and django work?
I have made several projects with JAVA/SPRING, and the recent days,
I am studying Python django .
I successed to get connection between django and sqlite, and few days ago,
when I tried to connect to mongodb, it is hard to seperate dependencies database and django because of django-admin.
Django-admin require to attain specific Fields to each model, however, every database has each their own Field properties. Therefore, when project should change database structure, we should change a lot of code in model.py.
e.g
What if sqlite to Mongodb? only using different driver cannot make server work. e.g models.TextField(sqlite) -> models.StringField(mongodb). It is unavoidable, right? It seems difficult that solating dependencies completely between database and django
Is it Okay? do I miss something?
What if sqlite to Mongodb? only using different driver cannot make server work. e.g models.TextField(sqlite) -> models.StringField(mongodb). It is unavoidable, right? It seems difficult that solating dependencies completely between database and django
TextField and StringField isn't where you will have problems since it is supported in almost all databases, but consider this library django-mongodb-engine, it reuses the basic fields that already exist in Django ORM to the extent of what's possible, e.g.
class Post(models.Model):
title = models.CharField() <-- both Django ORM and mongodb-engine have this
text = models.TextField() <-- both Django ORM and mongodb-engine have this
This model will work as is both in SQL and MongoDB engines, but if you use a MongoDB-only (as in not in default Django ORM engine) feature like ListField()
class Post(models.Model):
title = models.CharField()
text = models.TextField()
tags = ListField() # <-- not supported everywhere
Then you migration to a SQL engine will involve some manual work to map that field.
Therefore, when project should change database structure, we should change a lot of code in models.py. Is it Okay?
Typically yes, to change the database structure you make changes in models.py. After which you would be running a migration, which is a step that generates scripts to alter the database (e.g. adding a new SQL table column). For MongoDB there's no explicit migration, as its schema is flexible (or schemaless if you prefer).
As for the admin, it's fairly smart when it comes to Django models, you may need to define the models and fields you want to appear in the admin, but in the detail view you don't have to worry about every single field, as Django admin understands them based on the information in models.py
And to change database drivers, it's the DATABASES value in settings.py
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.
I created website-app for myself and it become really useful and many peoples wont also use it. But user management was not scheduled in application architecture. Is there any way to easy add user registration so each user would have his own google database tables but with same name?
example of one "table":
class Settings(db.Model):
email = db.StringProperty()
link = db.LinkProperty()
rating = db.StringProperty()
How can I separate data from this "table" between different users? I search for some kind of wrapper so I don't need to change current architecture.
You have to remember there is no concept of tables with the datastore so you can't have a separate set of tables for each user as such.
You have a few choices, the two I would investigate are
create a separate app with the existing code base and each user runs their own site. You may not need to do any code changes at all
If you want complete separation of data for each user in a single app then look at namespaces (thats how multi-tenancy is normally implemented.)
However you haven't really provided a clear definition of how you want to separate the users etc.. so there a probably other approaches you can take.
Short of having a copy of the database for each user; you'll have to implement some code changes.
The easiest one you can do is add a foreign key to your data tables that points to your user table; then you filter all records based on this foreign key.
Once you have that change, you can write a view decorator that will automatically filter the records for you.
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...