I am new to django and i tried changing tag field from char field to TaggableManager()
models.py
class UserBookmark(models.Model):
user = models.ForeignKey(User)
bookmark = models.URLField()
tag = TaggableManager()
def __str__(self):
return '%i %s %s'%(self.id,self.user,self.bookmark)
when i run python manage.py migrate, i get this error:
ValueError: Cannot alter field bookmark.UserBookmark.tags into
bookmark.UserBookmark.tags - they are not compatible types (you cannot
alter to or from M2M fields, or add or remove through= on M2M fields)
How can i remove this error?
I was able to solve similar issue by deleting the migration files and sqlite db.
Make sure to backup your db before deleting
Related
I am trying to migrate a database, but every time I try it returns the error below. My app name is 'helloservice', but the table is 'customer' in the database 'pulse'. How do I tell Django not to use the helloservice prefix? If needed I followed a tutorial here, and am now trying to adapt the results to my own needs.
django.db.utils.ProgrammingError: (1146, "Table 'pulse.helloservice_customer' doesn't exist")
Use Meta class and set table name. Then the table name only set customer. It does not use prefix(app name). Then make migrations and set migrate.
class Customer(models.Model):
customer_email = models.EmailField()
password = models.CharField(max_length=20)
ccustomer_first_name = models.CharField(max_length=30)
customer_last_name = models.CharField(max_length=30)
class Meta:
db_table = "customer"
I use to run my website on my laptop and its database was Sqlite, recently I wanted to transfer it to DigitalOcean and I changed its database to Postgresql, but when I migrate I encounters with some problems.
Python 3.4
Django 1.8
Error
django.db.utils.ProgrammingError: multiple default values specified for column "id" of table "profiles_userprofile"
My Model
class UserProfile(models.Model):
user = models.OneToOneField(User)
avatar = models.ImageField(blank=True, upload_to=get_image_path, default='/static/image/avatar/male.png')
age = models.IntegerField(default=4, validators=[MinValueValidator(3), MaxValueValidator(99)])
What should I do?
Try explicitly specifying the id field and marking it as the primary key:
class UserProfile(models.Model):
id = models.BigIntegerField(primary_key = True)
user = models.OneToOneField(User)
avatar = models.ImageField(blank=True, upload_to=get_image_path, default='/static/image/avatar/male.png')
age = models.IntegerField(default=4, validators=[MinValueValidator(3), MaxValueValidator(99)])
Django should automatically create a sequence for this field.
It may be that the User foreign key without an explicitly defined primary key is confusing the ORM, although that's just a theory.
If you are developing locally, and don't care about your migration history, and you just need a quick-fix, you could do this:
manage.py migrate <your app name> zero
Then delete the migration files except for the __init__.py in <your app name>.
Finally:
manage.py makemigrations <your app name>
manage.py migrate
A more complicated approach would be learning how to write and modify migration files to populate existing rows.
I've been stuck on this for a while and have gone through all of the suggestions already posted on here and it still won't recognize the column.
So I'm getting this:
DatabaseError at /admin/rango/category/add/
table rango_category has no column named views
Request Method: POST
Request URL: http://127.0.0.1:8000/admin/rango/category/add/
Django Version: 1.5.4
Exception Type: DatabaseError
Exception Value:
table rango_category has no column named views
Here is my models.py for rango
from django.db import models
class Category(models.Model):
name = models.CharField(max_length=128, unique=True)
views = models.IntegerField(default=0)
likes = models.IntegerField(default=0)
class Meta:
verbose_name_plural = "Categories" # otherwise writes "Categorys"
def __unicode__(self):
return self.name
class Page(models.Model):
category = models.ForeignKey(Category)
title = models.CharField(max_length=128)
url = models.URLField() # can include max_length if desired
views = models.IntegerField(default=0)
def __unicode__(self):
return self.title
I've already tried deleting the DB using python manage.py flush then doing python manage.py syncdb and it gives the same error. When I do python manage.py sql rango, I get:
BEGIN;
CREATE TABLE "rango_category" (
"id" integer NOT NULL PRIMARY KEY,
"name" varchar(128) NOT NULL UNIQUE,
"views" integer NOT NULL,
"likes" integer NOT NULL
)
;
CREATE TABLE "rango_page" (
"id" integer NOT NULL PRIMARY KEY,
"category_id" integer NOT NULL REFERENCES "rango_category" ("id"),
"title" varchar(128) NOT NULL,
"url" varchar(200) NOT NULL,
"views" integer NOT NULL
)
;
... so I'm pretty sure the column named "views" is there. Any help would be much appreciated!
Are you using SQLite? Look at the warning in this section of your tutorial:
http://www.tangowithdjango.com/book/chapters/models.html#creating-and-synchronising-the-database
Whenever you add to existing database models, you will have to delete the database file and then re-sync the database by running python manage.py syncb again. This is a drawback of Django 1.5.4, and can be quite frustrating. If you however add a new model, you can syncdb your database without having to delete and recreate it. You must therefore bear this in mind when tweaking your database: new models will be synchronised with syncdb - but changes to existing models will not be.
So, I'd delete the sqlite file, then run manage.py syncdb again.
Note: Django 1.7 is so much better, and there is a beta tutorial for it online.
I've completely wiped all my database tables in order to add a new field to a model since wiping SQL tables clean is the fastest way to do so without going the 'south' route when the SQL data only contains dummy data for testing purposes.
So here's my Model:
class Student(models.Model):
uid = models.ForeignKey(User)
title = models.CharField(max_length=250, help_text='The student name.')
resume = models.FileField(upload_to=get_upload_resume_name)
location = models.ForeignKey(Location)
country = models.ForeignKey(Country)
prim_interest = models.CharField(max_length=250, help_text='Maximum 250 characters.')
sec_interest = models.CharField(max_length=250, help_text='Maximum 250 characters.')
cellphone = models.IntegerField(default=0)
email_verified = models.BooleanField(default=False)
thumbnail = models.FileField(upload_to=get_upload_file_name)
def __unicode__(self):
return self.title
and the last field that I've added called 'thumbnail' is not being created by Django when I call syncdb right after deleting all my tables.
My method of completely wiping the tables has always worked no matter what drastic changes are applied to models.py and suddenly this is not the case for this instance. I could show you more code snippets but I have no clue where else in the Django project has a direct effect on generating models.
What could be causing syncdb to be refusing to write this new field 'thumbnail' to the Student table in the DB?
Could be That south is interfering. You habe no migrations waiting. If I remember well syncdb dos not create tables That south is scheduled to create.
Why not make a south migrate after your syncdb?
I am getting the following error:
IntegrityError: duplicate key value violates unique constraint "users_userprofile_pkey"
I am migrating from MySQL to Postgres, so I am dumping the data from the MySQL database using:
python2.7 manage.py dumpdata --indent=4 --natural > dump.json
I get the error when I attempt to load the dump.json into the Postgresql database:
python manage.py loaddata dump.json
I have the following signals in my users/model:
post_save.connect(create_user_profile, sender=User, dispatch_uid="user_create_profile")
post_save.connect(create_api_key, sender=User, dispatch_uid="user_create_api_key")
I had to comment out the post_save signals and then do the loaddata.
the problem may be because of the --natural keyword, if you read the docs about dumpdata natural keys here you will see it has some problems that apply to your database migration.
Also here they talk about a solution to this problem which seems to be pretty interesting (and tedious).
You can always try to add first the models that doesn't depend on each other so you can be sure they exists before trying to create another one. i.e:
if you have this models:
class Person(models.Model):
first_name = models.CharField(max_length=100)
last_name = models.CharField(max_length=100)
birthdate = models.DateField()
class Meta:
unique_together = (('first_name', 'last_name'),)
class Book(models.Model):
name = models.CharField(max_length=100)
author = models.ForeignKey(Person)
Then migrate the Person class first and then the book class.
Also if you can post the error it would be very helpful (since I could be more specific) but I am pretty certain the problem is a primary key issue