I have a model like this
class Task(models.Model):
name = models.CharField(max_length=100)
...
class TaskForm(forms.ModelForm):
class Meta:
model = Task
Then,I added couple of fields to Task and did migration
python manage.py schemamigration myapp --initial
python manage.py migrate myapp
Migration is successfully done.
Now,as an afterthought,I added a help_text to the model field
class Task(models.Model):
name = models.CharField(max_length=100,help_text='choose a good one')
...
Do I have to do the schemamigration again?I think his change doesn't affect the database table.
You need not run migrate again.
Django do not save help_text to the databases, help_text exists in application level, Django render it to HTML.
Related
I write this form for the product, that's got all brand and then set choice list for brand input
class ProductForm(forms.ModelForm):
class Meta:
BRAND_CHOICE = Brand.objects.all()
model = Product
fields = '__all__'
widgets = {
'brand': forms.Select(choices=BRAND_CHOICE),
}
but I take error when run
python manage.py migrate
an error that I taken
django.db.utils.OperationalError: no such table: app_product_brand
So how can I check DB and if tables exist then make a query to Database?
You can check in your models.py app_product_brand , if you have this model , if you are already having do
python manage.py makemigrations
python manage.py migrate app_product_brand
If not you can check all tables using this
from django.db import connection
all_tables = connection.introspection.table_names()
I want to use Django 1.7 for a new project.
and I already have database with many records.
In many Django tutorials,
it demo how to use migration system from a fresh new project.
In my case, use django-admin startapp todo
and will use a existed table named notesnote.
I use inspectdb to dump notesnote class and write it into todo/models.py
class NotesNote(models.Model):
title = models.CharField(max_length=100)
text = models.TextField()
pub_date = models.DateTimeField()
authors = models.CharField(max_length=10)
and then
python manage.py makemigrations todo
to generate todo/migrations/0001_initial.py
then
python manage.py migrate --fake todo
do a fake migrate(cause the table already existed).
Then, If I want to amend the table's field, say add a "category" field
category = models.CharField(max_length=30)
Then generate the 0002 migration diff by:
python manage.py makemigrations todo
However, when I do the migrate by
python manage.py migrate todo
I got error as below:
django.db.utils.OperationalError: no such table: todo_notesnote
Seems it add the app's name in front of the existed table.
Which steps should I do to make a usable migrations for existed table?
The documentation about integrating Django with a legacy database contains some useful advices for your use case: in particular you should add a db_table = 'notesnote' option to the inner Meta class of your model.
I have a model with managed=False in its Meta class.
class Modelname(models.Model):
class Meta:
managed=False
When I execute python manage.py dumpdata I get :
CommandError: Unable to serialize database: no such table: recruiterbox_modelname
How should I backup data from database using Django ?
My model:
class Article(models.Model):
user = models.ForeignKey(User)
categories = models.ManyToManyField(AuditGroup)
topic = models.ManyToManyField(Topic)
title = models.CharField(max_length=255)
short_desc = models.TextField(blank=True)
The migration created:
class Migration(SchemaMigration):
def forwards(self, orm):
# Adding model 'Article'
db.create_table('certification_article', (
('id', self.gf('django.db.models.fields.AutoField')(primary_key=True)),
('user', self.gf('django.db.models.fields.related.ForeignKey')(to=orm['auth.User'])),
('title', self.gf('django.db.models.fields.CharField')(max_length=255)),
('short_desc', self.gf('django.db.models.fields.TextField')(blank=True)),
))
None of the two many-to-many relationships are being produced! What am I missing?
Note: there's a strange thing in my model (took over the project): There is a class ProgramOverview in my model.py. But all code in this class is lowercase! In fact running --auto on schemamigration produces errors about ProgramOverview. Removing it, south wants to delete this class (turns out this is a view in the DB which is needed!) --> This seems to have been put there for some "hacky-ish" reason...So I produced the migration with:
./manage.py schemamigration certification --add-model Article
EDIT: This is the real problem. Somehow my editor messed up the ProgramOverview code. After restoring the code, I was able to run ./manage.py schemamigration certification --auto which produced all the needed tables!
END EDIT
I need the many-to-many though.
You wouldn't see an M2M in that declaration. What you see is correct.
An M2M "field" is an abstraction for a new table. There is no database level field in the 'Article' model.
Scan down the page to see the relevant M2M table creation code.
I have model Foo which resides inside app bar. Now, I wish to move thmodel to app bar2. I am already using db_table when syncdb with bar before
meta:
db_table = 'foo_table'
Now when I do schemamigration with bar, south wants me to delete the table. Is there any ways I can avoid this (table name foo_table is still the same despite changing the app) without manually editing the migration file?
if no changes in database, then you can create empty migrations for both apps in which was this model and which now has this model:
./manage.py schemamigration app1 del_model1 --empty
./manage.py schemamigration app2 add_model1 --empty
south analyze models which described in last migration and on this data he create next migrations