I want to create the admin for some tables of an existing MySQL dabatase. I want to use Django since it is just one line of code - admin.site.register(TableName) - so I save a lot of time coding the admin from scratch. I looked here for the inspectdb option of Django https://docs.djangoproject.com/en/1.5/howto/legacy-databases/ but only in the development version https://docs.djangoproject.com/en/dev/howto/legacy-databases/ says something about managing the inspected model. I want to know if it is possible and if somebody tried to work with an existing database before but not for read only, I want to manage and edit these database from my Django application.
If you're not using the development version, don't read the docs for that version. All they say is that - new in that version only - models created via inspectdb will by default be marked as managed = False, which means that they can't be modified. But the docs go on to describe exactly how you can modify them, simply by changing that setting.
But of course, you should be using the latest stable version, 1.5, which doesn't do that anyway. You can add and edit items in models from a legacy db to your heart's content.
Related
I have a blog website using django and I keep editing blogs occasionally, and I want to retrieve a history version at any time just like git and notes history function in evernote.
How can I do that? Should I save every new version in the database?
Are there any good solutions? Any language is welcome (python, java, ...).
Example:https://blog.evernote.com/blog/2010/04/14/new-premium-features-note-history-and-50mb-notes/
Yeah this can be done by adding a Django LogEntry. LogEntry is the model used by Django to maintain the Django admin edit history. You can use the same model to track changes to your blog.
Refer to this Stackoverflow answer on how to use it.
https://stackoverflow.com/a/988202/1774657
Django/Python version:
I would make two models: FirstBlog() and EditedBlog() and bind them via OnetoMany together.
everytime you edit the FirstBlog() version, you create another EditedBlog() version with information: who edited it, when edited, what edited.
In ASP.NET there is entity framework or something called "database first," where entities are generated from an existing database. Is there something similar for Django?
I usually work with a pre-existing database that I need to create a backend (and subsequently a front end) for. Some of these relational databases have many tables and relations so manually writing models isn't a good idea. I've scoured Google for solutions but have come up relatively empty handed.
You can use the information on this link.
python manage.py inspectdb > models.py
https://docs.djangoproject.com/en/3.0/howto/legacy-databases/
It depends on your database, but I've worked with it and is good.
The default in Django is a Code First-type approach where the Django framework engine creates a Db for you based on your models and uses migrations to update the Db with model changes (like Code First).
https://docs.djangoproject.com/en/1.11/intro/tutorial02/
What you're describing sounds like a Database First approach in the .Net world.
Integrating Django with a legacy database:
https://docs.djangoproject.com/en/1.11/howto/legacy-databases/
I am following a tutorial on Udemy on Django, in which the author adds a new model to models.py and runs the syncdb command to add the table to the database. I am trying to replicate this using Django 1.6.5 and MySQL 5.5. I have searched other questions and they are slightly different scenarios.
I read in the docs that you can add new models using syncdb, but you will either have to drop the existing tables or use South to migrate the data to alter an existing table. I am simply trying to add a new model, and I get the error -- the table already exists.
Could someone explain why I am getting this error, and should I just use South and avoid this situation?
Once you've created the DB, you can't simply add new models.
In order to update your schema, you'll have to use south (for migrations in Django <= 1.6), or use the built-in migrations in Django 1.7
Since you're using Django 1.6, you should read about it in http://south.readthedocs.org/en/latest/tutorial/part1.html and then use the package to migrate your schema.
Just follow the tutorial and ask on Stack Overflow if further questions rise, it shouldn't be too complicated
We are 2 developers working on a django application. As we are in the initial stages of the app our models are changing rapidly and so are django migration files. So if one of us pushes the migrations to git and when the other one pulls them and tries to apply them, django is not able to find a common migration point as he also would have made some model changes. Please suggest a clean way to resolve this issue.
so there are developers A and B.
if it is very important you both have notification enabled in git so that you know what your friend has pushed.
lets imagine, A and B are working at the same time on same django app, possibly on the same models.py.
if A pushes changes on models.py, B needs to apply these changes in his local version before he pushes. if B pushes his changes, A needs to apply them first before he pushes. in this way, both local versions will have the same migration history. this is how I do with my friends..
any improvement to what i say is highly appreciated
I've written some python code to accomplish a task. Currently, there are 4-5 classes that I'm storing in separate files. I'd now like to change this whole thing into a database-backed web app. I've been reading tutorials on Django, and so far I get the impression that I'll need to manually specify the fields and their types for every "model" that I use. This is a little surprising to me, since I was expecting some kind of ORM capability that would just take the existing classes I've already defined, and map them onto a database somehow, in a manner abstracted away from me.
Is this not the case? Am I missing something? It looks like I need to specify all the fields and types in the file 'models.py'.
Okay, now beyond those specifics, does anyone have any general tips on the best way to migrate an object-oriented desktop application to a web application?
Thanks!
That is Django's ORM: it maps classes to tables. What else did you expect? There needs to be some way of specifying what the fields are, though, before you can use them, and that's managed through the models.Model class and the various models.Field subclasses. You can certainly use your classes as mixins in order to use the existing business logic on top of the field definitions.
If you are thinking about a database backend based web app, you have to specify what fields of the data you want to store and what type of the value you want stored.
There is an abstraction that introspects the db to convert it into the django models.py format. But I know not of any that introspects a python class and stores arbitrary data into db. How would that even work? Are the objects, now, stored as a pickle?
You're going to have to check the output, but you can have Django automatically create models from existing databases through one-time introspection.
Taken from the link below, you would set up your database in settings.py, and then call
python manage.py inspectdb
This will dump the sample models.py file to standard out for your inspection. In order to create the file, simply redirect the output
python manage.py inspectdb > models.py
See for more:
http://docs.djangoproject.com/en/dev/howto/legacy-databases/?from=olddocs#auto-generate-the-models