0

当 django 视图执行模型对象的 get 函数时,我有一个 MigrationErroruser_profile = UserProfile.objects.get(user=request.user)

例外情况如下:

HTTP GET /media/website/bg_chat.PNG 200 [0.22, 127.0.0.1:1417]
Internal Server Error: /chat/French
Traceback (most recent call last):
  File "C:\Users\Ahmed\Desktop\MS_ ISI\PA\Mini_Projet\ISI_Chat\venv\lib\site-packages\djongo\sql2mongo\query.py", line 287, in _align_results
    ret.append(doc[selected.column])
KeyError: 'numberOfBan'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\Ahmed\Desktop\MS_ ISI\PA\Mini_Projet\ISI_Chat\venv\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
    response = get_response(request)
  File "C:\Users\Ahmed\Desktop\MS_ ISI\PA\Mini_Projet\ISI_Chat\venv\lib\site-packages\django\core\handlers\base.py", line 126, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "C:\Users\Ahmed\Desktop\MS_ ISI\PA\Mini_Projet\ISI_Chat\venv\lib\site-packages\django\core\handlers\base.py", line 124, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\Ahmed\Desktop\MS_ ISI\PA\Mini_Projet\ISI_Chat\venv\lib\site-packages\django\contrib\auth\decorators.py", line 21, in _wrapped_view
    return view_func(request, *args, **kwargs)
  File "C:\Users\Ahmed\Desktop\MS_ ISI\PA\Mini_Projet\ISI_Chat\chat\views.py", line 32, in chatroom
    uprofile = UserProfil.objects.get(user=user)
  File "C:\Users\Ahmed\Desktop\MS_ ISI\PA\Mini_Projet\ISI_Chat\venv\lib\site-packages\django\db\models\manager.py", line 82, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "C:\Users\Ahmed\Desktop\MS_ ISI\PA\Mini_Projet\ISI_Chat\venv\lib\site-packages\django\db\models\query.py", line 393, in get
    num = len(clone)
  File "C:\Users\Ahmed\Desktop\MS_ ISI\PA\Mini_Projet\ISI_Chat\venv\lib\site-packages\django\db\models\query.py", line 250, in __len__
    self._fetch_all()
  File "C:\Users\Ahmed\Desktop\MS_ ISI\PA\Mini_Projet\ISI_Chat\venv\lib\site-packages\django\db\models\query.py", line 1186, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
  File "C:\Users\Ahmed\Desktop\MS_ ISI\PA\Mini_Projet\ISI_Chat\venv\lib\site-packages\django\db\models\query.py", line 63, in __iter__
    for row in compiler.results_iter(results):
  File "C:\Users\Ahmed\Desktop\MS_ ISI\PA\Mini_Projet\ISI_Chat\venv\lib\site-packages\django\db\models\sql\compiler.py", line 1005, in apply_converters
    for row in map(list, rows):
  File "C:\Users\Ahmed\Desktop\MS_ ISI\PA\Mini_Projet\ISI_Chat\venv\lib\site-packages\django\db\models\sql\compiler.py", line 1466, in cursor_iter
    for rows in iter((lambda: cursor.fetchmany(itersize)), sentinel):
  File "C:\Users\Ahmed\Desktop\MS_ ISI\PA\Mini_Projet\ISI_Chat\venv\lib\site-packages\django\db\models\sql\compiler.py", line 1466, in <lambda>
    for rows in iter((lambda: cursor.fetchmany(itersize)), sentinel):
  File "C:\Users\Ahmed\Desktop\MS_ ISI\PA\Mini_Projet\ISI_Chat\venv\lib\site-packages\django\db\utils.py", line 96, in inner
    return func(*args, **kwargs)
  File "C:\Users\Ahmed\Desktop\MS_ ISI\PA\Mini_Projet\ISI_Chat\venv\lib\site-packages\djongo\cursor.py", line 59, in fetchmany
    ret.append(self.result.next())
  File "C:\Users\Ahmed\Desktop\MS_ ISI\PA\Mini_Projet\ISI_Chat\venv\lib\site-packages\djongo\sql2mongo\query.py", line 719, in __next__
    return next(self._result_generator)
  File "C:\Users\Ahmed\Desktop\MS_ ISI\PA\Mini_Projet\ISI_Chat\venv\lib\site-packages\djongo\sql2mongo\query.py", line 725, in __iter__
    yield from iter(self._query)
  File "C:\Users\Ahmed\Desktop\MS_ ISI\PA\Mini_Projet\ISI_Chat\venv\lib\site-packages\djongo\sql2mongo\query.py", line 180, in __iter__
    yield self._align_results(doc)
  File "C:\Users\Ahmed\Desktop\MS_ ISI\PA\Mini_Projet\ISI_Chat\venv\lib\site-packages\djongo\sql2mongo\query.py", line 290, in _align_results
    raise MigrationError(selected.column)
djongo.sql2mongo.MigrationError: numberOfBan

和 UserProfile 模型:

class UserProfile(models.Model):
    SEXE = (
        ('M', 'Male'),
        ('F', 'Female')
    )
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    DateBirth = models.DateTimeField(blank=True, null=True)
    photo = models.ImageField(upload_to=UploadTo)
    gender = models.CharField(max_length=10, choices=SEXE)
    #New fields
    numberOfBan = models.IntegerField(blank=True, null=True)
    lastBan = models.DateTimeField(blank=True, null=True)

    def __str__(self):
        return self.user.username

要知道:

  • 首先,我使用了默认数据库 sqLite,之后我切换到 noSQL 数据库 mongoDB,一切正常。

  • 在我向 userProfile 模型添加一些字段后,我运行了迁移命令并且工作正常。

  • 我检查了数据库的集合,它在某些文档中具有“lastBan”字段和“int32”而不是“numberOfBan”,而另一个则根本没有新字段!

  • 当我添加一个新的用户配置文件时,它在文档中有两个字段,但存在相同的异常问题。

请问有什么想法吗??

4

0 回答 0