I am trying to use a same table across two different projects and want to reflect the changes between them.
Let's assume that there is a student table in the data created by PROJECT1-APP1 of django. I want that same student table to use in PROJECT2-APP1. I have searched the interned and get recommendation to import the model from 1 project to other but according to my understanding it is just "faking" it and some functionality like BigAutoFeild and Foreign Keys would not work in this solution. Is there a way that I can use the same database in two different projects which will not mess up with the core operation?
EDIT:
the other questions just shows how to connect database which I know, the problem occurs when I try to use the same tables.
You may need to create a model in the other app and explicitly specify the database table in the class Meta section. Similar to below:
class Person(models.Model):
id = models.IntegerField(primary_key=True)
first_name = models.CharField(max_length=70)
class Meta:
db_table = 'table_persons'
I'm new to Django and I am trying to use a mysql database created and filled with data by someone else
I created a model with the same name as the table I want to get data from, my models is as follows
class Study(models.Model):
study_name = models.TextField(default='Unknown')
description = models.TextField(default='Unknown')
language = models.TextField(default='Unknown')
number_of_years = models.IntegerField(default='0')
database connected but when I go to admin I don't see the data there
Please help me with this
A step by step solution would be:
get the name of the table containing your data, I'll call it study_table
make sure you know how the table was defined so you can match it with django model definition. Connect to the database with a MySQL client and run the following query:
DESCRIBE study_table;
based on the table name, column types and column names, define your model to match everything. Django models do a lot of automated naming so you have force the naming to make sure your model matches your database. Principles are:
Specify the table name as a meta option.
Create fields with names matching column names and field types matching column types. Taking an example from your code, the field study_name should match a column with the same name in the table study_table.
class Study(models.Model):
study_name = models.TextField(default='Unknown')
description = models.TextField(default='Unknown')
language = models.TextField(default='Unknown')
number_of_years = models.IntegerField(default=0)
class Meta:
db_table = study_table
Side note: your IntegerField has a default as a string '0'.
making sure the app (I'll call it study_app) containing your model is enabled, the database is configure properly in your django settings, try to access data from the admin shell (python manage.py shell):
>>> from study_app.models import Study
>>> Study.objects.first()
This should return an answer, if it does not, your model doesn't match the database data.
to make accessing the data easier, create an admin page as suggested by #iklinac. You can now read, edit your data through your browser.
A few suggestions you could consider:
study_name should probably be a models.CharField(max_length=255) or similar
description should be allowed to be empty models.TextField(blank=True)
language should probably be a models.CharField with a choices option.
You should create ModelAdmin instance for your model
The ModelAdmin class is the representation of a model in the admin
interface. Usually, these are stored in a file named admin.py in your
application.
from django.contrib import admin
from myproject.myapp.models import Study
class StudyAdmin(admin.ModelAdmin):
pass
admin.site.register(Study, StudyAdmin)
If you have a MySQL database with tables of data that don't have models created yet, you can use the dumpdata command to automatically generate the models:
https://docs.djangoproject.com/en/3.0/ref/django-admin/#dumpdata
Then, you can register those models in the Django admin. dumpdata should only be used as a starting point, since they are auto-generated and won't contain many of Django's data integrity features.
Good luck!
We are trying to makemigrations and migrating the app level model to respective databases using database router.
We have one model file in one app pointing to one database and same structured models with table name with some tables added are being created in other app then intermediate table name is creating error.
Error : master.LegalTbl.legal_field_name: (field.E340) The field's intermediary table 'tbl_legal_tbl' clashes with the table name of 'user.LegalTbl'.
I'm not sure may be it could work:
delete the migration file that has conflict.
add Class Meta to your model which name you want to change
meta class have one attribute called db_table write table name
Now you can run makemigrations and migrate, it will solve your conflict i guess
I am new to django, so please excuse if I am totally wrong.
I have a django installation in which some tables are manually imported from outside source. There is one table with large number of fields. In my current django I need to interact with only few of its fields.
Can I create a django model for that table with just the fields I need and will it work? Will it mess up migrations completely? How is such a situation usually handled in django?
You can use meta option db-table and managed
class ModelWithFewFields(models.Model):
# Fields declare here
class Meta:
db_table = 'Real_DB_TABLE_NAME'
managed = False
In the same MySQL server we have a database for each client. This databases share the same table structure. We were able to create a Model for table Foo that looks like this:
class Foo(models.Model):
id = models.AutoField(primary_key=True)
bar = models.CharField(max_length=50)
class Meta:
managed = False
db_table = 'foobar'
Our Django project needs to manage all of our clients. As all of them have the same structure we would also like to share the Foo model. At the begging we were able to handle this issue by defining in the project settings each client's database and using routers. At the moment we have too many clients to have them all defined in setting.py.
Is there any reasonable way to tell the model which schema has to be used every time we use it?