I just switched out the django User model for a CustomUser model. I've got a field called contributor_id in a Project model which is a m2m field connected to the User model. I've told the m2m field to point to the CustomUser model and have run makemigrations and migrate but the user_id field has not changed to customuser_id. This is causing the following error:
Unknown column 'projects_project_contributor_id.customuser_id' in 'field list'
I found these bug reports: bug1 and bug2 but they both appear to have been fixed in 2.0. Obviously I can just run an alter table query on my database, but I don't know if:
a) There's away to get the django orm to do the change, and
b) if altering the tables directly will get my migrations out of wack.
Related
I have a model which is named Client. It's defined in models in part as the following:
class Client(models.Model):
username = models.OneToOneField(settings.AUTH_USER_MODEL,
on_delete=models.CASCADE)
I've run makemigrations and migrations, but when I create a new client object and try to save it from forms I get and Integrity Error: NOT NULL constraint failed: intake_client.username_id.
username_id is the field name Django automatically generates with the migration, but saving these forms results in this field containing a null value and is not being generated automatically as a dependency by Django. If I set primary key to true in the username, it works ok, but then I have problems deleting the objects because Django says the model does not have an id field (since it is now changed to username_id). Why is Django not generating and putting the dependency values in the username_id field automatically when the instance is saved?
I needed to change my foreign key on my User model from a UUIDField to an IntegerField. I did this in two steps:
Rename the current primary key from id to old_id and run a migration. This generated a RenameField operation.
Deleted the old_id field entirely from User so that the automatic id field would take over. This generated a RemoveField operation on old_id and an AddField operation for a models.AutoField named id. Perfect. The database shows that User now has an int id field, auto-incrementing.
Then I went to run the app and quickly ran into a problem: the database join tables automatically generated by the models.ManyToManyFields on User (e.g. user_languages weren't updated - they still have both a language_id AND a user_id that are UUID data types. There is no foreign key constraint in the database, but there are indexes on that field.
How can I force Django to regenerate those join tables with the new data type for the User.id column?
Notes: manage.py makemigrations doesn't pick up any pending changes. Also, I'm OK with losing the data in the db.
Remove/comment out fields from the model:
class User(models.Model):
# languages = models.ManyToManyField('Language')
# other fields
Then generate a migration. Then uncomment the line(s). Then run another migration.
Warning: This will cause those tables to be dropped and re-created, so all those relationships will be lost.
I have already many models in my application.
I have already used migrations in my project.
But when I want to remove a field from a model, the makemigration command show me :
Unknown field(s) (field) specified for Model
Where (fields) is equal to the field's name and Model is the model's name ?
Do you have any solution to resolve this issue ?
You may try modifying auto_now and auto_now_add with default = 'xxx', then the field can be called
i had a profile field in todo models which a ForeignKey to Profile model
Instead of using profile i want to use the user field of User model as a ForeignKey .
Steps:
I have removed the profile field from todo model, created and applied migration for that.
Works fine, profile field do not exist in database.
2, I added user field as a ForeignKey in todos models .
When doing python manage.py schemamigration todos --auto
gives response like:
Nothing seems to have changed.
what am missing here ?
I created a form based on a model. The model has a many2many field. I defined the field like this:
contacts = models.ManyToManyField(Contact, blank=True, null=True)
I`m wondering now why the generated form says that this field cannot be blank. I always get the error message "This field is required.", when i do not select a contact for the contacts field.
Whats`s wrong?
In your form declaration mark this field as required=False
class MyForm(forms.ModelForm):
contacts=forms.ModelMultipleChoiceField(queryset=Contact.objects.all(),required=False)
class Meta:
model=MyModel
Possibly you did syncdb before adding blank=True, null=True?
syncdb will only create tables if they don't exist in the database. Changes to models have to be done manually in the database directly with SQL or using a migration tool such as South.
Of course, if you are still in early development, it will be easier to drop the database and run syncdb again.
Your use of null=True is confusing here. A manyToMany field results in a 3rd table relating one model to another. e.g.
Business <-> Contact
If business.contacts is empty, no records are entered into this table. null=True would make me think you are intending for NULL records to be added to this table, which doesn't seem valid.
Typically you would leave both of these attributes off.