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 ?
Related
I have a Django application with a My-SQL database. recently I alter the table_name with the help of MySQL query in the MySQL-shell, after this when I run makemigration and migrate command terminal says "No changes detected". how can i resolve this issue and create again this table with help of Django makemigration and migrate?
can I delete a table from MySQL, any possibility will Django create it again?
If you renamed your table outside Django - you will have to tell Django the new table name like so (using the Meta class):
class Model(models.Model):
name = models.CharField(max_length=255)
class Meta:
db_table = 'new_table_name'
To re-create your table using existing model you need to reset migration for that app to zero and then run migration again
python manage.py migrate APP_NAME zero
python manage.py migrate APP_NAME
It's because the migrations table managed by django doesn't reflect the correct db schema since it was already modified outside of django. If you don't have any important data you can do a migration rollback or recreate the table by hand.
The best way to dela with this is to rename your table back to the original name. Then create a blank migration inside your app and recreate the sql commands you did in the shell inside that migration file. That way django can keep track of the database schema.
You should change the name of the table in models.py not in MySQL shell.
From
class MyModel(models.Model):
...
To
class ThisModel(models.Model):
...
Or Create Proxy Model :
class ThisModel(MyModel):
class Meta:
proxy = True
verbose_name = "ThisModel"
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'm using django MPTT model.
I subclass MPTT model, but then try to add fixture to the custom model with supplied initial_data in JSON.
The parent TreeForeignKey is optional (blank=True, null=True)
When I apply a JSON fixture from initial_data, it asks to supply the fields "lft", "rght", "tree_id", "level".
ie: may not be NULL
...when running python manage.py syncdb
These are fields from MPTT.
Is there a way to exclude this or get around this from the fixture data?
Thanks
Previously I had a django model like this
class Review(models.Model):
reviewdate=models.DateField(default=date.today)
description=models.TextField()
author=models.ForeignKey(User,null=True)
I have some 500 records of Review in db.
I added a field to model
from django.core.validators import MinValueValidator,MaxValueValidator
class Review(models.Model):
reviewdate=models.DateField(default=date.today)
description=models.TextField()
author=models.ForeignKey(User,null=True)
rating= models.IntegerField(validators=[MinValueValidator(1), MaxValueValidator(10)], default=5, help_text='integers 1 to 10')
I ran python manage.py schemamigration myapp --auto successfully ,which created a 0002_auto__add_field_review_rating.py file
Now, I need to do the datamigration for the existing records in db. Do I have to run
python manage.py datamigration myapp somechanges
and then implement the functions in the created somechanges.py ? Since I have already defined in the new field difficulty a default value of 5, will that not be taken when migrate command is run? Do I have to explicitly set it in the somechanges.py functions?
You do not need to do a data migration for this case. As you have specified a default for your new field, that will be used when you apply the schema migration.
Use the following command to apply the migration:
./manage.py migrate myapp
See the advanced changes South tutorial for more information on default values for new fields.
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.