如何删除Django两个模型之间的许多关系

时间:2022-10-05 14:21:19
class Question(models.Model):
      q_title = models.CharField(max_length=500)
      q_user = models.ForeignKey(MyUser, null=False, blank=False)
      q_slug = models.SlugField(null=True, blank=True)
      q_category=  models.ManyToManyField('Categories',null=True,blank=True)

class Categories(models.Model):
    category = models.CharField(max_length=200)

How do I delete q_category from Question Model. It gives me the error " Cannot alter field questions.Question.q_category into questions.Question.q_category - they are not compatible types (you cannot alter to or from M2M fields, or add or remove through= on M2M fields)"

如何从问题模型中删除q_category。它给出了错误“无法将字段问题.Question.q_category改为问题。问题.q_category - 它们不兼容类型(您无法更改到M2M字段或从M2M字段更改,或在M2M字段上添加或删除)”

1 个解决方案

#1


1  

if you are still in development at the start of it just delete your db, delete your migrations and delete q_cateregory from Question model and re-create db again: run makemigrations, migrate and syncb.

如果您在开始时仍在开发中,只需删除数据库,删除迁移并从问题模型中删除q_cateregory并再次重新创建数据库:运行makemigrations,migrate和syncb。

Otherwise, just start from reading this: https://docs.djangoproject.com/en/1.8/ref/migration-operations/

否则,请从阅读本文开始:https://docs.djangoproject.com/en/1.8/ref/migration-operations/

#1


1  

if you are still in development at the start of it just delete your db, delete your migrations and delete q_cateregory from Question model and re-create db again: run makemigrations, migrate and syncb.

如果您在开始时仍在开发中,只需删除数据库,删除迁移并从问题模型中删除q_cateregory并再次重新创建数据库:运行makemigrations,migrate和syncb。

Otherwise, just start from reading this: https://docs.djangoproject.com/en/1.8/ref/migration-operations/

否则,请从阅读本文开始:https://docs.djangoproject.com/en/1.8/ref/migration-operations/