B
B
becks2014-05-21 16:08:00
MySQL
becks, 2014-05-21 16:08:00

How to show data from model connected to mysql in django admin?

I understand with django I came across another problem, the solution of which I could not find. Launched 'hello word' - admin panel with a list of groups and users, everything is edited, updated, deleted. Used sqlite3 (blank).
I decided to change sqlite to mysql and display the table from the mysql database in the admin panel. What could be simpler :)
mysql database already contains data. What I did:
1) Added a new database to DATABASES:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': 'Archive',
        ...
    },
    'istok': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'istok',
        'USER': 'root',
        'PASSWORD': 'root',
    }
}
2) Installed MySQL-python.
3) Created (generated) a model (I do not take all the fields from the table):
class Doc(models.Model):
    channel_number = models.CharField(max_length=2, blank=True)
    session = models.DateTimeField(blank=True, null=True)
    size = models.IntegerField(blank=True, null=True)
    suffix = models.CharField(max_length=10, blank=True)
    class Meta:
        managed = False
        db_table = "archive"
4) Registered the model in the admin panel
class DocAdmin(admin.ModelAdmin):
    list_display = ('channel_number ', 'session ', 'size ', 'suffix')

# Register your models here.
admin.site.register(Doc, DocAdmin)
5) I launch the admin panel, the section appeared, but if I click on Docs or on the change option, it crashes with OperationalError at /admin/Docs/doc/ no such table: archive. By Add, it opens the form with the addition of a new entity (although it also falls when adding) Therefore, tell me
what I'm doing wrong, what needs to be done? What is not clear to me:
1) How does django understand which database the archive table belongs to. As I understand it, now she refers it to the default one, how to specify the base in this case? (It so happened that the name of the default database is the same as the name of the table of the second database)
2) How to completely move from SQLite, i.e. remove it from the project?
3) How and where does django store a list of users and their passwords, did you find an explanatory description anywhere?
4) How to show not all the data from the model, but, for example, split it into 20 pieces?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
F
freydev, 2014-05-22
@becks

1. Django always works with the "default" base, it doesn't even take into account others. To explicitly specify which base to work with, you need to use the using parameter for all base methods, this can be done in 2 ways: define DATABASE_ROUTES or override models.Model methods
https://docs.djangoproject.com/en/dev/topics/db/mu ...
2. I think you've already figured it out
3. Users are stored in the auth_user table, along with password hashes, unless of course you override the user table, https://docs.djangoproject.com/en/1.6/topics/auth/ ... there is something about
users and backends
4. Very simple, Doc.objects.all()[:5] will add limit 5 to the request, or use the Paginator object through class-based views

B
becks, 2014-05-21
@becks

Removed the sqlite database. Named mysql database "default" and executed syncdb. Works.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question