小编dir*_*ten的帖子

DRF 更改自定义操作的默认视图集的 lookup_field

如何在 DRF 视图集中更改自定义操作的默认查找参数?这是我的视图集(简化)

class InvitationViewSet(MultiSerializerViewSet):   
    queryset = Invitation.objects.all()  

    @action(
        detail=False,
        #url_path='accept-invitation/<str:key>/',
        #lookup_field='key'
    )
    def accept_invitation(self, request, key=None):
        invitation = self.get_object()
        with legal_invitation(invitation):
            serializer = self.get_serializer(invitation)
            invitation.accepted = True
            invitation.save()
        return Response(serializer.data)
Run Code Online (Sandbox Code Playgroud)

我希望当用户输入 url 时/invitations/accept-invitation/abccba,其中abccba是随机令牌字符串。key- 是邀请模型中的一个独特字段。我知道我可以设置 per-Viewset lookup_field='key',但我希望所有其他操作仍然使用 default lookup_field='pk'。我怎样才能达到我想要的?

python django django-rest-framework

6
推荐指数
1
解决办法
2082
查看次数

ValueError:字段“id”需要一个数字但得到“处理”

我将在 DigitalOcean 上部署我的 django 应用程序。一切顺利,除了以下错误,我的问题是:我在哪里可以找到这个错误的来源,实际上是在哪个文件中?

Operations to perform:
  Apply all migrations: admin, auth, ccapp, contenttypes, sessions
Running migrations:
  Applying ccapp.0009_auto_20191207_2148...Traceback (most recent call last):
  File "/home/progbash/ccproject/env/lib/python3.6/site-packages/django/db/models/fields/__init__.py", line 1768, in get_prep_value
    return int(value)

ValueError: invalid literal for int() with base 10: 'Processing'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "manage.py", line 21, in <module>
    main()
  File "manage.py", line 17, in main
    execute_from_command_line(sys.argv)
  File "/home/progbash/ccproject/env/lib/python3.6/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
    utility.execute()
  File "/home/progbash/ccproject/env/lib/python3.6/site-packages/django/core/management/__init__.py", …
Run Code Online (Sandbox Code Playgroud)

python django

4
推荐指数
4
解决办法
5万
查看次数

在Ubuntu 18.04上为python安装mysqlclient时出错

我在Ubuntu上安装了Python 2.7.15rci和Python 3.6.7。当我在virtualenv上执行“点列表”时,它返回我:

Django (2.1.5)
pip (9.0.1)
pkg-resources (0.0.0)
pytz (2018.9)
setuptools (39.0.1)
wheel (0.32.3)
Run Code Online (Sandbox Code Playgroud)

我正在尝试安装mysqlclient(pip install mysqlclient)并返回错误。

  unable to execute 'x86_64-linux-gnu-gcc': No such file or directory
  error: command 'x86_64-linux-gnu-gcc' failed with exit status 1

  ----------------------------------------
  Failed building wheel for mysqlclient
  Running setup.py clean for mysqlclient
Failed to build mysqlclient
Installing collected packages: mysqlclient
  Running setup.py install for mysqlclient ... error
    Complete output from command /home/david/env/project/bin/python3 -u -c "import setuptools, tokenize;__file__='/tmp/pip-build-pq18uxjj/mysqlclient/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /tmp/pip-y28h4ou0-record/install-record.txt --single-version-externally-managed …
Run Code Online (Sandbox Code Playgroud)

mysql django pip mysql-python python-3.x

3
推荐指数
3
解决办法
1891
查看次数

用户管理器对象没有属性 create_superuser

问题:使用 manage.py 时,我无法创建超级用户,并且它以错误响应 AttributeError: 'UserManager' object has no attribute 'create_superuser’。然后我尝试手动导入所有必要的模型并在 python shell 中运行它,但遇到了相同的障碍。

目标:使用继承的基本管理类正确创建超级用户

代码:

from django.contrib.auth.models import AbstractUser, BaseUserManager, Group

class UserManager(BaseUserManager):
    def get_by_natural_key(self, username):
        return self.get(username__iexact=username)

class User(AbstractUser):
    objects = UserManager()

    …
    …

    def __str__(self):
         return ’{}’.format(self.id)

    def save(self, *args, **kwargs):
        self.full_clean()
        super(FollowUser, self).save(*args, **kwargs)

Run Code Online (Sandbox Code Playgroud)

python django django-models

1
推荐指数
1
解决办法
1880
查看次数

如何使用 django-cors-headers 从数据库动态填充 CORS_ORIGIN_WHITELIST?

我正在编写一个 Django 应用程序,其中有一个名为 的模型Website,其中包含人员网站。我只允许在我的数据库中拥有网站的人使用我的 Django REST API。我正在使用该django-cors-headers包将人们的域列入白名单: https: //github.com/adamchainz/django-cors-headers

CORS_ORIGIN_WHITELISTsettings.py 中的变量允许我将域列入白名单,如https://github.com/adamchainz/django-cors-headers#cors_origin_whitelist所示

问题是我必须查询我的模型以获取网站域,将它们附加到列表中,然后将该列表放入CORS_ORIGIN_WHITELIST. 但我无法在 settings.py 中执行此操作,因为模型是在应用程序启动后加载的,而 settings.py 是启动应用程序的模型。

有谁知道解决办法吗?任何建议将不胜感激。提前致谢。

python django

1
推荐指数
1
解决办法
2014
查看次数