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 ?
Related
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.
After customizing my user model in Django Oscar, I received the following error message:
IntegrityError at /
insert or update on table "basket_basket" violates foreign key constraint "basket_basket_owner_id_74ddb970811da304_fk_auth_user_id"
DETAIL: Key (owner_id)=(5) is not present in table "auth_user".
To customize my user model, I followed the instructions here.
First, I wrote the following models.py file, located within my project directory at apps/user/models.py.
from django.db import models
from oscar.apps.customer.abstract_models import AbstractUser
from django.contrib.postgres.fields import ArrayField
class User(AbstractUser):
acct_bal = models.DecimalField(max_digits=10, decimal_places=2, default=0.00)
purchased_items = ArrayField(models.IntegerField(), default=list)
The idea is that I want the user to have an account balance (which I will use for payment later) as well as a list of product numbers representing items that have already been purchased.
After making models.py, I edited the installed apps as follows:
INSTALLED_APPS = [...
'shopworld.apps.user',
] + get_core_apps()
And then put this at the bottom of my settings.py:
AUTH_USER_MODEL = 'user.User'
I then did ./manage.py migrate, but for some reason I am getting this error message. I also tried dropping the django_admin_log table as suggested here, but it did not work. Any help would be greatly appreciated.
I fixed this - the issue was that I was trying to migrate to a custom user model after already having done migrations with auth_user. This meant that auth_user didn't update correctly. I had to flush and re-sync the database, so that the initial migration captured the custom user model.
So i have this model where I added user field as foreign key to User model
from django.db import models
from django.contrib.auth.models import User
from django.conf import settings
class Post(models.Model):
url = models.URLField()
title = models.CharField(max_length=140)
user = models.ForeignKey(settings.AUTH_USER_MODEL)
Than I run
python3 manage.py makemigrations
And get:
You are trying to add a non-nullable field 'user' to comment without a default;
we can't do that (the database needs something to populate existing rows).
Please select a fix:
1) Provide a one-off default now (will be set on all existing rows)
2) Quit, and let me add a default in models.py
I understand that I need to give it default user but I don't get how to do that.
P.S. Django version - 1.8.3
Ok, so it happens migration utility's question hadn't appeared intuitive enough for me. What migration utility was really asking were required fields of User model. So basically you need to type in some data 3 times (for username, password and email). The problem is utility isn't really saying what field is it expecting.
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
I have changed django.contrib.auth.user model where I have made the email id to be unique. After which I added a datamigration to reflect the same-
python manage.py datamigration appname unique_password.
Followed by python manage.py schemamigration appname --auto-->no changes. Finally, python manage.py migrate appname.
Migrating forwards to 0003_unique_password
appname:0037_unique_password.py
Now when I add a user whose email is not unique, it gives me an error and does not let me create the user through the django admin. But when i do:
>
python manage.py shell
from django.contrib.auth.models import User
user=User('cust1','123','xxx#gmail.com')
This creates a user object even though 'xxx#gmail.com' already exists.
How can I add an unique constraint to django.contrib.auth.user.email for an existing project (with data)?
Checks for uniqueness occur during model validation as per the Model validation documentation. Calling save() on the model doesn't automatically cause model validation. The admin application will be calling model validation.
Nothing prevents you from creating a User object in Python such as user=User('cust1','123','xxx#gmail.com') and then doing a user.save(). If there are uniqueness checks at the database level, you'll likely get an exception at this point. If you want to check before doing the save, call is_valid() on the Model instance.
If you want every User object to be automatically validated before save, use the pre_save Django signal to do Model validation before the save proceeds.