Using 'old' database with django - python

I'm using a hand built (Postgres) database with Django. With "inspectdb" I was able to automatically create a model for it. The problem is that some tables have multiple primary keys (for many-to-many relations) and they are not accessible via Django.
What's the best way to access these tables?

There is no way to use composite primary keys in Django's ORM as of now (up to v1.0.2).
I can only think of three solutions/workarounds:
There is a fork of django with a composite pk patch at github that you might want to try.
You could use SQLAlchemy together with Django.
You have to add a single field primary key field to those tables.

Django does have support for many-to-many relationships. If you want to use a helper table to manage this relationships, the ManyToManyField takes a through argument which specifies the table to use. You can't model anything terribly complex this way, but it is good for most simple applications.

Related

It's possible to have relationships between models stored in different databases in Django?

I'm currently working on a project where I handle both public and private information, both stored as different models in a common database.
I would like to split this database in two, one with the private model objects and another one with the public ones.
The thing is, both this models have a ForeignKey relationship with each other, and I've found conflicting answers over the internet about if this relationships can work even if the models are in two different databases.
So, is this possible? Is there a better approach for doing this?
Just to clarify why I want to do this, I want the project to be open source, therefore the public database should be public, but the sensitive information (users and passwords) should be kept private.
From Django docs:
Django doesn’t currently provide any support for foreign key or many-to-many relationships spanning multiple databases. If you have used a router to partition models to different databases, any foreign key and many-to-many relationships defined by those models must be internal to a single database.
This is because of referential integrity. In order to maintain a relationship between two objects, Django needs to know that the primary key of the related object is valid. If the primary key is stored on a separate database, it’s not possible to easily evaluate the validity of a primary key.
For possible solutions check out this discussion: https://stackoverflow.com/a/32078727/14209813

Handling many-to-many relationship from existing database using Django ORM

I'm starting to work with Django, already done some models, but always done that with 'code-first' approach, so Django handled the table creations etc. Right now I'm integrating an already existing database with ORM and I encountered some problems.
Database has a lot of many-to-many relationships so there are quite a few tables linking two other tables. I ran inspectdb command to let Django prepare some models for me. I revised them, it did rather good job guessing the fields and relations, but the thing is, I think I don't need those link tables in my models, because Django handles many-to-many relationships with ManyToManyField fields, but I want Django to use that link tables under the hood.
So my question is: Should I delete the models for link tables and add ManyToManyFields to corresponding models, or should I somehow use this models?
I don't want to somehow mess-up database structure, it's quite heavy populated.
I'm using Postgres 9.5, Django 2.2.
In many cases it doesn't matter. If you would like to keep the code minimal then m2m fields are a good way to go. If you don't control the database structure it might be worth keeping the inspectdb schema in case you have to do it again after schema changes that you don't control. If the m2m link tables can grow properties of their own then you need to keep them as models.

Is it necessary to use `relationship()` in SQLAlchemy?

I've noticed that many SQLAlchemy tutorials would use relationship() in "connecting" multiple tables together, may their relationship be one-to-one, one-to-many, or many-to-many. However, when using raw SQL, you are not able to define the relationships between tables explicitly, as far as I know.
In what cases is relationship() required and not required? Why do we have to explicitly define the relationship between tables in SQLAlchemy?
In SQL, tables are related to each other via foreign keys. In an ORM, models are related to each other via relationships. You're not required to use relationships, just as you are not required to use models (i.e. the ORM). Mapped classes give you the ability to work with tables as if they are objects in memory; along the same lines, relationships give you the ability to work with foreign keys as if they are references in memory.
You want to set up relationships for the same purpose as wanting to set up models: convenience. For this reason, the two go hand-in-hand. It is uncommon to see models with raw foreign keys but no relationships.

Using Composite Primary keys with Django

I am using Django 1.6, and i want to use composite primary keys. I know that its not directly supported by Djnago. But is there any alternative or packages to implement that. Any help would be appreciated. Thanks.
You can use Django with SQLAlchemy instead of the native ORM for models where you need composite primary keys.
You will not be able to use the admin app with SQLAlchemy models without a surrogate key (this is probably the case with most of the Django ecosystem). You will also need a ModelForm replacement like WTForms.
If you need composite primary keys, take a look at Flask instead, You will be able to leverage most of your Django skills. It is very well documented and has a sizeable ecosystem.

Django lookup tables and fields

Can I look around in a database with Django? I mean outputting a list of all tables, all fields in tables and all objects in a specific table?
I know how to build models and lookup objects, but can I lookup fields in a preexisting database? I have a bit experience with unmanaged django models, but if I should use those, then I have to create models for alle the tables and specify each field. I think it should be possible to just look around in a database without 'managing it'. At least there must be a existing project on github or such with the functionality? Thank you.
You want manage.py inspectdb (https://docs.djangoproject.com/en/1.5/howto/legacy-databases/#auto-generate-the-models)
You can use raw SQL queries to execute statements like Show Table

Categories

Resources