扩展现有用户模型后,关系不存在

时间:2018-12-06 05:08:25

标签: django django-models

我遵循django docs来扩展现有的用户模型。我已经完成了makemigrations并进行了迁移。当我尝试获取client_id或client_site_id时,它返回错误。我的代码中是否缺少任何内容或任何错误?

这是我的模型。py:

class UserProfile(models.Model):
    user = models.OneToOneField(User,on_delete=models.CASCADE)
    client_id = models.ForeignKey(Client, models.DO_NOTHING, db_column='Client_Id')
    client_site_id = models.ForeignKey(ClientSite, models.DO_NOTHING, db_column='Client_Site_Id')

这是我的views.py-,用于获取client_id

user = User.objects.get(username='James')
info = user.userprofile.client_id

错误:

File "C:\Python\Python36\lib\site-packages\django\db\models\fields\related_descriptors.py" in __get__
  392.             rel_obj = self.related.get_cached_value(instance)

File "C:\Python\Python36\lib\site-packages\django\db\models\fields\mixins.py" in get_cached_value
  13.             return instance._state.fields_cache[cache_name]

During handling of the above exception ('userprofile'), another exception occurred:

File "C:\Python\Python36\lib\site-packages\django\db\backends\utils.py" in _execute
  85.                 return self.cursor.execute(sql, params)

The above exception (relation "customers_userprofile" does not exist
LINE 1: ...entId", "customers_userprofile"."Client_Site_Id" FROM "customers...
                                                             ^
) was the direct cause of the following exception:

File "C:\Python\Python36\lib\site-packages\django\core\handlers\exception.py" in inner
  34.             response = get_response(request)

File "C:\Python\Python36\lib\site-packages\django\core\handlers\base.py" in _get_response
  126.                 response = self.process_exception_by_middleware(e, request)

File "C:\Python\Python36\lib\site-packages\django\core\handlers\base.py" in _get_response
  124.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "C:\Users\client\views.py" in View
  136.     info = user.userprofile.client_id

File "C:\Python\Python36\lib\site-packages\django\db\models\fields\related_descriptors.py" in __get__
  400.                     rel_obj = self.get_queryset(instance=instance).get(**filter_args)

File "C:\Python\Python36\lib\site-packages\django\db\models\query.py" in get
  393.         num = len(clone)

File "C:\Python\Python36\lib\site-packages\django\db\models\query.py" in __len__
  250.         self._fetch_all()

File "C:\Python\Python36\lib\site-packages\django\db\models\query.py" in _fetch_all
  1186.             self._result_cache = list(self._iterable_class(self))

File "C:\Python\Python36\lib\site-packages\django\db\models\query.py" in __iter__
  54.         results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)

File "C:\Python\Python36\lib\site-packages\django\db\models\sql\compiler.py" in execute_sql
  1065.             cursor.execute(sql, params)

File "C:\Python\Python36\lib\site-packages\django\db\backends\utils.py" in execute
  100.             return super().execute(sql, params)

File "C:\Python\Python36\lib\site-packages\django\db\backends\utils.py" in execute
  68.         return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)

File "C:\Python\Python36\lib\site-packages\django\db\backends\utils.py" in _execute_with_wrappers
  77.         return executor(sql, params, many, context)

File "C:\Python\Python36\lib\site-packages\django\db\backends\utils.py" in _execute
  85.                 return self.cursor.execute(sql, params)

File "C:\Python\Python36\lib\site-packages\django\db\utils.py" in __exit__
  89.                 raise dj_exc_value.with_traceback(traceback) from exc_value

File "C:\Python\Python36\lib\site-packages\django\db\backends\utils.py" in _execute
  85.                 return self.cursor.execute(sql, params)

Exception Type: ProgrammingError at /details/
Exception Value: relation "client_userprofile" does not exist
LINE 1: ...entId", "client_userprofile"."Client_Site_Id" FROM "client...

我的数据库引擎:PostgreSQL

2 个答案:

答案 0 :(得分:0)

您需要做的是:

     user=User.objects.get(username='James')
     userprofile=UserProfile.objects.get(user=user)

然后做:

     userprofile.client_id.idfield

client_id是另一个Client表的外键,因此,要获取特定字段,必须执行client_id。(id字段)

答案 1 :(得分:0)

您必须必须在扩展userProfile模型中创建用户实例。因此,将扩展用户配置文件更改为:

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 Profile(models.Model):
        user = models.OneToOneField(User, on_delete=models.CASCADE)
        client_id = models.ForeignKey(Client, models.DO_NOTHING,db_column= 'Client_Id')
        client_site_id = models.ForeignKey(ClientSite, models.DO_NOTHING, db_column='Client_Site_Id')
        def __str__(self):
            return self.user.username
    @receiver(post_save, sender=User)
    def create_user_profile(sender, instance, created, **kwargs):
        if created:
            Profile.objects.create(user=instance)
    @receiver(post_save, sender=User)
    def save_user_profile(sender, instance, **kwargs):
        instance.profile.save()