我看到有一个__format__方法,但是help(int.__format__)没有提供任何帮助。
我也知道您不应该__method__直接致电。什么时候调用此方法?它的论点是什么?
我有一些模型都有一些共同的功能.每个模型都是物理库存类型的项目,因此他们分享类似的东西stock,并分享一些事情,如低库存警告事件(发送电子邮件).
我没有复制代码,而是编写了一个抽象模型并从中继承.
class LowStockModel(models.Model):
stock = stock = models.IntegerField()
out_of_stock_behaviour = models.CharField(max_length=20, choices=[...])
class Meta:
abstract = True
def save(self, *args, **kwargs):
super(self.__class__, self).save(*args, **kwargs)
if self.stock <= 0:
#...
Run Code Online (Sandbox Code Playgroud)
我的问题是我需要更改标签或添加不同于我正在产生的子类中help_text的stock字段.这非常重要,因为客户(及其员工)需要有关单位的说明.我已经尝试过在孩子身边徘徊,__init__但我没有到达任何地方.
tl; dr如何从子模型中更改字段上的标签?
我有一个自定义用户模型,除了电子邮件和密码之外还包含许多字段.一个字段user_type设置为设计人员或开发人员.其他字段特定于一种或另一种类型.
我需要为每个用户类型都有一个单独的注册表单.
使用自定义字段设置一个注册表单很容易使用django-allauth,因为我可以使用该ACCOUNT_SIGNUP_FORM_CLASS设置.我不确定如何设置多个.
我正在Django 1.7中进行操作,并尝试将一个数据库字段is_dispensing从现有字段迁移BooleanField到NullBooleanField。
我的迁移文件:
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models, migrations
class Migration(migrations.Migration):
dependencies = [
('frontend', '0007_practice_is_dispensing'),
]
operations = [
migrations.AlterField(
model_name='practice',
name='is_dispensing',
field=models.NullBooleanField(),
preserve_default=True,
),
]
Run Code Online (Sandbox Code Playgroud)
运行manage.py migrate失败,并显示以下错误:
django.db.utils.IntegrityError: column "is_dispensing" contains null values
Run Code Online (Sandbox Code Playgroud)
我的模型文件中的字段:
is_dispensing = models.NullBooleanField(blank=True)
Run Code Online (Sandbox Code Playgroud)
以前是:
is_dispensing = models.BooleanField(null=True, blank=True)
Run Code Online (Sandbox Code Playgroud)
当我添加它时,要求我提供一个默认值,我将其设置为“无”。
我发现此消息令人困惑-我正在尝试将列类型迁移到NullBooleanField,为什么它不能包含空值?这就是该列类型的重点,不是吗?:)
更新:另一个令人困惑的事情是:如果我进入Postgres并查看应该具有该列的表,那么实际上它根本没有任何is_dispensing列。
我正在使用DRF进行休息,所以现在我正在对我的apis进行限制.为此,我创建了以下节流范围
userRateThrottle
anonRateThrottle
burstRateThrottle
perViewsThrottles(随视图而变化)
目前我得到的回应低于:
{"detail":"Request was throttled. Expected available in 32.0 seconds."}
我想回复这样的事情:
{"message":"request limit exceeded","availableIn":"32.0 seconds","throttleType":"type"}
DRF文档中没有任何内容可用于自定义.如何根据要求自定义我的回复?
我有一个 Django 应用程序,正在配置一些安全设置。其中一项设置是SESSION_COOKIE_HTTPONLY标志。我将此标志设置为 True。
在会话创建(登录)时,HTTPOnly如果我检查 cookie,我可以看到会话标志设置。注销时,服务器发回一个带有空值的会话 cookie 更新,以表明 cookie 已被销毁。此空 cookie 不会与httpOnly设置的标志一起发回。
我的问题:这是一个安全问题吗?有没有办法强制 Django 在注销时设置这个标志?或者这只是预期的行为,而不是安全问题,因为返回的会话 cookie 是空白的?
我试图返回特定于用户的单个对象(不是查询集)而不必在请求的URL中指定标识符/ pk.每个用户都有一个组织FK.
即http://网站/组织,而不是http:// website/organization/1
我收到以下错误,因为它期望这个标识符:
AssertionError: Expected view OrganisationDetail to be called with a URL keyword argument named "user__organisation_id". Fix your URL conf, or set the.lookup_fieldattribute on the view correctly.
在使用RetrieveModelMixin/GenericAPIView时,如何/我需要指定它返回由FK链接的单个对象?
我的观点类:
class OrganisationDetail(mixins.RetrieveModelMixin, mixins.UpdateModelMixin,generics.GenericAPIView):
serializer_class = OrganisationDetailSerializer
lookup_field = 'pk' #yes, I know this is the default and there's no need to speciy
def get_queryset(self):
return Organisation.objects.filter(pk=self.request.user.organisation_id)
def get(self, request, *args, **kwargs):
return self.retrieve(request, *args, **kwargs)
def put(self, request, *args, **kwargs):
return self.update(request, *args, …Run Code Online (Sandbox Code Playgroud) 如何解密 django 散列的 sha256 密码?
我有一个加密的密码:
pbkdf2_sha256$12000$laEnG5drNrAt$mFMVBvQh8YF+vriNXbe/Nb8eYEySWwPT5+oSaMPvUiA=
它相当于“管理员”。
现在我需要解密密码。意味着我想要“admin”作为输出。
这可能吗?
符号.py
class Symbol(BaseModel):
name = models.CharField(max_length=30,)
class Meta:
abstract = True
class StockSymbol(Symbol):
market = models.CharField(max_length=10,)
my_daily_price = GenericRelation(MyDailyPrice)
Run Code Online (Sandbox Code Playgroud)
每日价格.py
class DailyPrice(BaseModel):
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
content_object = GenericForeignKey('content_type', 'object_id')
class Meta:
abstract = True
class MyDailyPrice(DailyPrice):
open = models.DecimalField(
max_digits=15,
decimal_places=2,
)
Run Code Online (Sandbox Code Playgroud)
我想做的是,
symbol = StockSymbol.objects.first()
MyDailyPrice.objects.filter(content_object=symbol)
Run Code Online (Sandbox Code Playgroud)
但它发生了错误:
FieldError: Field 'content_object' does not generate an automatic reverse relation and therefore cannot be used for reverse querying. If it is a GenericForeignKey, consider adding a GenericRelation.
Run Code Online (Sandbox Code Playgroud)
StockSymbol已经有了 …
我有一个具有以下列表方法的视图集:
class PolicyViewSet(viewsets.ViewSet):
def list(self, request):
queryset = Policy.objects.all()
serializer = PolicySerializer(queryset, many=True)
return Response(serializer.data)
Run Code Online (Sandbox Code Playgroud)
这按预期工作,我得到了我想要的响应。但是,现在我试图限制每个GET请求返回的对象,为此我使用分页。我在 settings.py 中定义了以下内容:
REST_FRAMEWORK = {
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
'PAGE_SIZE': 20
}
Run Code Online (Sandbox Code Playgroud)
文档说:
只有在使用通用视图或视图集时才会自动执行分页。
但是,我的结果仍然没有分页。我还需要做什么才能启用分页?
python ×10
django ×9
django-orm ×2
foreign-keys ×1
httponly ×1
int ×1
pagination ×1
rest ×1
security ×1
throttling ×1