我需要创建一个内联formset
a)排除某些字段MyModel完全显示
b)显示一些字段MyModel但阻止它们可编辑.
我尝试使用下面的代码,使用values()以便将查询集过滤到我想要返回的那些值.但是,这失败了.
任何有想法的人?
class PointTransactionFormset(BaseInlineFormSet):
def get_queryset(self):
qs = super(PointTransactionFormset, self).get_queryset()
qs = qs.filter(description="promotion feedback")
qs = qs.values('description','points_type') # this does not work
return qs
class PointTransactionInline(admin.TabularInline):
model = PointTransaction
#formset = points_formset()
#formset = inlineformset_factory(UserProfile,PointTransaction)
formset = PointTransactionFormset
Run Code Online (Sandbox Code Playgroud) 我想在 SQL 形式的多个选择语句中使用相同的序列号,最终将其用作表中的 PK 插入并将多条记录联系在一起。
到目前为止,我只能从双中选择 NEXTVAL:
SELECT TEST_SEQ.NEXTVAL AS SEQ FROM DUAL;
Run Code Online (Sandbox Code Playgroud)
但是,当我将序列包含在多列选择中时,出现此处不允许序列错误。
SELECT col1, co2, col3, (select TEST_SEQ.NEXTVAL) SEQ
FROM table;
Run Code Online (Sandbox Code Playgroud)
任何帮助是极大的赞赏。
所以我对 Django 还很陌生,我似乎无法弄清楚这里发生了什么。我有一个正在运行并显示在网页中的表单,并且我能够创建一个数据库并在 SQL 中显示。但是,当我尝试获取表单以将信息保存到数据库中时,我开始测试的字段已经消失,并且没有任何内容写入数据库。
我也不断收到此错误:AttributeError: type object 'Team' has no attribute '_meta'
追溯:
Unhandled exception in thread started by <function wrapper at 0x108cbd050>
Traceback (most recent call last):
File "/Users/alicen/git/first_robotics/venv/lib/python2.7/site-packages/django/utils/autoreload.py", line 226, in wrapper
fn(*args, **kwargs)
File "/Users/alicen/git/first_robotics/venv/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 116, in inner_run
self.check(display_num_errors=True)
File "/Users/alicen/git/first_robotics/venv/lib/python2.7/site-packages/django/core/management/base.py", line 426, in check
include_deployment_checks=include_deployment_checks,
File "/Users/alicen/git/first_robotics/venv/lib/python2.7/site-packages/django/core/checks/registry.py", line 75, in run_checks
new_errors = check(app_configs=app_configs)
File "/Users/alicen/git/first_robotics/venv/lib/python2.7/site-packages/django/core/checks/urls.py", line 10, in check_url_config
return check_resolver(resolver)
File "/Users/alicen/git/first_robotics/venv/lib/python2.7/site-packages/django/core/checks/urls.py", line 19, in check_resolver
for pattern in resolver.url_patterns:
File …Run Code Online (Sandbox Code Playgroud) 如何使某些字段对于特定用户权限级别为只读?
有一个 Django REST API 项目。有一个Foo带有 2 个字段的序列化程序 -foo和bar. 有 2 个权限 -USER和ADMIN.
序列化器定义为:
class FooSerializer(serializers.ModelSerializer):
...
class Meta:
model = FooModel
fields = ['foo', 'bar']
Run Code Online (Sandbox Code Playgroud)
如何确保 'bar' 字段是只读的USER和可写的ADMIN?
我会使用 smth 像:
class FooSerializer(serializers.ModelSerializer):
...
class Meta:
model = FooModel
fields = ['foo', 'bar']
read_only_fields = ['bar']
Run Code Online (Sandbox Code Playgroud)
但是如何使其成为有条件的(取决于许可)?
最近我发现没有记录的django.db.models.fields.Field.name选项:
Run Code Online (Sandbox Code Playgroud)@total_ordering class Field(RegisterLookupMixin): # here we have it ... ????????? def __init__(self, verbose_name=None, name=None, primary_key=False, max_length=None, unique=False, blank=False, null=False, db_index=False, rel=None, default=NOT_PROVIDED, editable=True, serialize=True, unique_for_date=None, unique_for_month=None, unique_for_year=None, choices=None, help_text='', db_column=None, db_tablespace=None, auto_created=False, validators=(), error_messages=None): ...
有提到在DOC路吧:
Run Code Online (Sandbox Code Playgroud)# A guide to Field parameters: # # * name: The name of the field specified in the model. # * attname: The attribute to use on the model object. This is the same as # "name", except …
我正在学习 django 测试,我发现 django 工厂男孩库对于编写测试用例非常有帮助,但有一件事我没有得到..
例如我的工厂名称之一是BlogFactory
所以我注意到,大多数人这样使用它:BlogFactory.create()有些人这样使用它..BlogFactory.create_batch()我没有得到它之间的区别..
create和之间有什么不同create_batch?
我的应用程序具有以下网址条目:
url(r'^(?P<pk>\d+)/(?P<action>add|edit)/type/$', 'customer.views.edit', name='customer-edit'),
Run Code Online (Sandbox Code Playgroud)
我想反向发布到该网址。当我执行以下操作时,出现错误NoReverseMatch:
self.client.post(reverse('customer-edit'), {'something:'something'}, follow=True)
Run Code Online (Sandbox Code Playgroud)
这是完整的错误:NoReverseMatch:找不到带有参数“()”和关键字参数“ {}”的“ customer-edit”。
我需要反向传递args或kwargs吗?如果是这样,他们将如何匹配上面的网址?
我正在运行 Django、uwsgi、ngix 服务器。我的服务器适用于较小尺寸的 GET、POST 请求。但是在POST大尺寸的请求时,nginx返回502:nginx error.log是:
2016/03/01 13:52:19 [error] 29837#0: *1482 sendfile() failed (32: Broken pipe) while sending request to upstream, client: 175.110.112.36, server: server-ip, request: "POST /me/amptemp/ HTTP/1.1", upstream: "uwsgi://unix:///tmp/uwsgi.sock:", host: "servername"
Run Code Online (Sandbox Code Playgroud)
因此,为了找到真正的问题所在,我在不同的端口上运行 uwsgi 并检查同一请求是否发生任何错误。但是请求成功了。因此,问题出在 nginx 或 unix 套接字配置上。Ngin-x 配置:
# the upstream component nginx needs to connect to
upstream django {
server unix:///tmp/uwsgi.sock; # for a file socket
# server 127.0.0.1:8001; # for a web port socket (we'll use this first)
}
# configuration of the server
server …Run Code Online (Sandbox Code Playgroud) 从Python 3.7的新增功能中,
我们可以看到有新的math.remainder。它说
返回关于y的x的IEEE 754样式余数。对于有限x和有限非零y,这是差异
x - n*y,其中n是最接近商的精确值的整数x / y。如果x / y恰好位于两个连续整数之间,则将最接近的偶数整数用于n。因此,其余部分r = remainder(x, y)始终令人满意abs(r) <= 0.5 * abs(y)。特殊情况遵循IEEE 754:尤其
remainder(x, math.inf)是对于任何有限X X和remainder(x, 0)和remainder(math.inf, x)提高ValueError任何非NaN的X。如果余数运算的结果为零,则该零将具有与x相同的符号。在使用IEEE 754二进制浮点的平台上,此操作的结果始终可以精确表示:不引入舍入错误。
但我们也要记住,有一个%符号是
其余
x / y
我们还看到操作员需要注意:
不适用于复数。相反,
abs()如果合适,使用转换为浮点数。
我甚至没有尝试过运行python 3.7。
但是我尝试过
Python 3.6.1 (v3.6.1:69c0db5050, Mar 21 2017, 01:21:04)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin …Run Code Online (Sandbox Code Playgroud) 下面的代码在字典中搜索单词,并在search.html上呈现结果,所以我需要在该页面上对结果进行分页,我该怎么做?我在这里阅读了文章https://docs.djangoproject.com/en/1.9/topics/pagination/,但我不知道如何将分页代码嵌入到我的中。
def search(request):
if 'results' in request.GET and request.GET['results']:
results = request.GET['results']
word = words.objects.filter(title__icontains = results).order_by('title')
return render_to_response('myapp/search.html',
{'word': word, 'query': results })
else:
return render(request, 'myapp/search.html')
Run Code Online (Sandbox Code Playgroud) django ×8
python ×7
cpython ×1
django-admin ×1
django-forms ×1
django-tests ×1
django-urls ×1
factory-boy ×1
http-post ×1
math ×1
nginx ×1
oracle ×1
pagination ×1
python-3.7 ×1
regex ×1
sql ×1
sqlite ×1
uwsgi ×1