Python 将自定义字段添加到默认用户模型-如果扩展它,我应该执行迁移吗?

Python 将自定义字段添加到默认用户模型-如果扩展它,我应该执行迁移吗?,python,django,Python,Django,我一直在尝试用XP字段扩展用户模型。在编写代码之后(我认为它是正确的),我决定尝试一下 不幸的是,我遇到了以下错误: ProgrammingError at /register/ relation "accounts_userprofile" does not exist LINE 1: INSERT INTO "accounts_userprofile" ("user_id", "xp") VALUES ... ^ 我认为这是因为我扩展了用户模型,但没有运行

我一直在尝试用XP字段扩展用户模型。在编写代码之后(我认为它是正确的),我决定尝试一下

不幸的是,我遇到了以下错误:

ProgrammingError at /register/
relation "accounts_userprofile" does not exist
LINE 1: INSERT INTO "accounts_userprofile" ("user_id", "xp") VALUES ...
                 ^
我认为这是因为我扩展了用户模型,但没有运行迁移。 这是我的密码: views.py

models.py

from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.dispatch import receiver

class UserProfile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    xp = models.IntegerField(default=0)

@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
    if created:
        UserProfile.objects.create(user=instance)

@receiver(post_save, sender=User)
def save_user_profile(sender, instance, **kwargs):
    instance.profile.save()
当我运行python manage.py showmigrations时,我看不到此应用程序的任何迁移—这是我以“accounts”的名称创建的应用程序

我想我遗漏了一些东西,因为当我有另一个模型时,我做了一个迁移,但它没有像预期的那样工作。现在我看不到此应用程序的任何迁移,即使我从中删除了migrations文件夹

那么,错误是否来自未运行“迁移”?如果是,我应该如何运行它,因为我看不到它的任何迁移

谢谢。

你需要做些什么

python manage.py makemigrations
哪里是你的应用程序的名称, 这将使django检测到对模型的更改


python manage.py migrate
将更改提交到数据库

运行
python manage.py makemigrations
first@JibinMathews我发现“未检测到任何更改”do python manage.py makemigrations现在运行的应用程序[帐户]的名称在哪里。谢谢如果你愿意,你可以加上这个作为答案,所以我会接受它。
from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.dispatch import receiver

class UserProfile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    xp = models.IntegerField(default=0)

@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
    if created:
        UserProfile.objects.create(user=instance)

@receiver(post_save, sender=User)
def save_user_profile(sender, instance, **kwargs):
    instance.profile.save()