Answer the question
In order to leave comments, you need to log in
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. 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 panelclass 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 Answer the question
In order to leave comments, you need to log in
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
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question