简体   繁体   English

为什么我无法在 Django model 添加新字段? 我正在使用 docker 容器

[英]Why am I unable to add new field in Django model? I am using docker container

I am running the Django service in a docker container.我在 docker 容器中运行 Django 服务。 Django has a Postgres database remote access from AWS RDS, and all this is working fine. Django 从 AWS RDS 远程访问 Postgres 数据库,一切正常。 But I am adding a new field (amount) in Model.但我在 Model 中添加了一个新字段(金额)

class MyModel(models.Model):
    model_id = models.UUIDField(default=uuid.uuid4)
    model2 = models.ForeignKey(MyModel2, on_delete=models.CASCADE, related_name='mymodels')
    amount = models.FloatField(default=0) # this is new field
    updated_at = models.DateTimeField(auto_now=True)
    created_at = models.DateTimeField(auto_now_add=True)

MyModel table already exists in the database.数据库中已存在MyModel表。 price is new field here.价格是这里的新领域。 Whenever I start server and create migrations, new migrations are created 'accounts/migrations/0001_initial.py'.每当我启动服务器并创建迁移时,都会创建新的迁移“accounts/migrations/0001_initial.py”。

docker-compose build
docker-compose up -d

docker-compose exec djangoservice bash
~\ python manage.py makemigrations
~\ python manage.py migrate

after this process, no changes in my database table.在此过程之后,我的数据库表没有任何变化。 Can someone tell me what am I doing wrong?有人可以告诉我我做错了什么吗? How can I add new fields in my exesting table?如何在现有表中添加新字段?

Maybe you can consider deleting this file manually,I mean 0001_initial.py.也许你可以考虑手动删除这个文件,我的意思是 0001_initial.py。 Sometimes migration detection of django is not very effective.有时 django 的迁移检测不是很有效。

To get reflect all changes in the local repo that occurred in the docker container's code repository, I added a volume to sync the container with the local repo.为了反映 docker 容器的代码存储库中发生的本地存储库中的所有更改,我添加了一个卷以将容器与本地存储库同步。

services:
  web:
    build: ./app
    ports:
      - "8000:8000"
    volumes:
      - ./app:/home/app_dir # this volume
    command: gunicorn djangoproject.wsgi:application --workers=2 --bind 0.0.0.0:8000
    expose:
      - 8000

Now build and up the container;现在构建容器;

docker-compose exec web bash
/app$ cd /home/app_dir
/app$ python manage.py makemigrations

After executing these commands you'll see all migration files will be added to local migration folders.执行这些命令后,您会看到所有迁移文件都将添加到本地迁移文件夹中。 Now commit these files to the version control repo.现在将这些文件提交到版本控制仓库。 ie, git commit.即,git 提交。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我无法向 django 模型的对象添加数据 - Why am I unable to add data to django Model's objects 我无法在 Django 中扩展用户 model - I am Unable to extend the User model in Django 我无法显示Markdown字段中的表格,并且正在使用django-markdownx - I am unable to show table from markdown field and I am using django-markdownx 为什么我无法通过“docker-compose run web”命令运行 django 迁移? - Why am I unable to run django migrations via the 'docker-compose run web' command? 我无法在 Django 中创建新的自定义用户 - I am unable to create a new custom user in Django 为什么在提交时无法为表单字段分配值? - Why am I unable to assign a value to a form field on submit? 我正在尝试向默认 Django 表单添加一个新字段,当我尝试从我制作的 forms.py 文件导入 class 时出现错误 - I am trying to add a new field to the default Django Form and am getting an error when I'm trying to import a class from a forms.py file I made 我无法在包含大量文本的Django模型字段上使用索引。 我正在使用MySQL。 我该怎么办? - I am not able to use indexing on Django model field which contains large amount of text. I am using MySQL. What should I do? 我无法使用 Django 发送 email 什么? - What am I unable to send email using Django? 使用django-allauth我无法使用Twitch登录 - Using django-allauth I am unable to log in with Twitch
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM