升级到Django 1.8(使用zc.buildout)并运行syncdb或migrate时,我收到以下消息:
django.db.utils.ProgrammingError: relation "auth_user" does not exist
我的一个模型包含django.contrib.auth.models.User:
user = models.ForeignKey(
User, related_name='%(app_label)s_%(class)s_user',
blank=True, null=True, editable=False
)
Run Code Online (Sandbox Code Playgroud)
降级到Django 1.7可以消除错误.我是否必须在Django 1.8中以不同方式包含User对象?
我想django-rest-framwork在保存之前编辑序列化程序对象.这就是我目前的做法 -
def upload(request):
if request.method == 'POST':
form = ImageForm(request.POST, request.FILES)
if form.is_valid(): # All validation rules pass
obj = form.save(commit=False)
obj.user_id = 15
obj.save()
Run Code Online (Sandbox Code Playgroud)
如何使用django-rest-framework序列化程序对象执行此操作?
@api_view(['POST','GET'])
def upload_serializers(request):
if request.method == 'POST':
serializer = FilesSerializer(data=request.DATA, files=request.FILES)
if serializer.is_valid():
serializer.save()
Run Code Online (Sandbox Code Playgroud) python django serialization django-serializer django-rest-framework
我在Django REST框架中有一个序列化程序,定义如下:
class QuestionSerializer(serializers.Serializer):
id = serializers.CharField()
question_text = QuestionTextSerializer()
topic = TopicSerializer()
Run Code Online (Sandbox Code Playgroud)
现在我有两个使用上述序列化程序的API视图:
class QuestionWithTopicView(generics.RetrieveAPIView):
# I wish to include all three fields - id, question_text
# and topic in this API.
serializer_class = QuestionSerializer
class QuestionWithoutTopicView(generics.RetrieveAPIView):
# I want to exclude topic in this API.
serializer_class = ExamHistorySerializer
Run Code Online (Sandbox Code Playgroud)
一种解决方案是编写两个不同的序列化器.但必须有一个更容易的解决方案来有条件地从给定的序列化器中排除一个字段.
python django serialization django-serializer django-rest-framework
我有一个具有可选字段的对象.我用这种方式定义了我的序列化器:
class ProductSerializer(serializers.Serializer):
code = serializers.Field(source="Code")
classification = serializers.CharField(source="Classification", required=False)
Run Code Online (Sandbox Code Playgroud)
如果它不存在,我认为 required=False可以绕过该字段.但是,文档中提到这会影响反序列化而不是序列化.
我收到以下错误:
'Product' object has no attribute 'Classification'
Run Code Online (Sandbox Code Playgroud)
当我尝试访问.data序列化实例时会发生这种情况.(这是不是意味着反序列化提高了这个?)
这种情况发生在没有的情况下Classification.如果我省略Classification了序列化程序类,它可以正常工作.
我该如何正确地做到这一点?使用可选字段序列化对象,即.
情况
在Django REST框架中使用验证时ModelSerializer,我注意到这些Meta.model字段总是经过验证,即使这样做不一定有意义.以下示例为User模型的序列化:
password领域和一个confirm_password领域.如果两个字段不匹配,则无法创建用户.同样,如果请求username已存在,则无法创建用户.validate在串行的情况下(见下文),捕不匹配password和confirm_password领域执行validate:
def validate(self, data):
if data['password'] != data.pop('confirm_password'):
raise serializers.ValidationError("Passwords do not match")
return data
Run Code Online (Sandbox Code Playgroud)
问题
即使ValidationError被引发validate,ModelSerializer仍然会查询数据库以检查它username是否已被使用.这在从端点返回的错误列表中很明显; 存在模型和非字段错误.
因此,我想知道如何在非字段验证完成之前阻止模型验证,从而节省了对数据库的调用.
尝试解决方案
我一直试图通过DRF的来源找出这种情况发生的地方,但是我找不到我需要覆盖的内容以使其工作失败.
如果我有一个不可为空的模型字段,删除它,并创建一个迁移,那么迁移将变为不可逆:
考虑以下模型:
class Foo(models.Model):
bar = models.TextField()
test = models.TextField() # This field is to go away, bye-bye!
Run Code Online (Sandbox Code Playgroud)
和迁移:
# app/migrations/003_remove_foo_test.py
class Migration(migrations.Migration):
dependencies = [
('app', '0002_foo_test'),
]
operations = [
migrations.RemoveField(
model_name='foo',
name='test',
),
]
Run Code Online (Sandbox Code Playgroud)
取消应用此迁移会引发异常:
$ src/manage.py migrate app 0002
Operations to perform:
Target specific migration: 0002_foo_test, from app
Running migrations:
Unapplying app.0003_remove_foo_test...Traceback (most recent call last):
...
django.db.utils.IntegrityError: column "test" contains null values
Run Code Online (Sandbox Code Playgroud)
当然,这是预期的行为,它是清楚记录的,我不是在问为什么会发生这种情况:
请记住,当反转时,这实际上是在模型中添加一个字段; 如果该字段不可为空,则可能使该操作不可逆转(除了任何数据丢失,这当然是不可逆转的).
但是,我们都会犯错误,有时我们只需要以某种方式反转字段删除,即使这意味着手动为所有反转的非空字段提供临时存根值.例如,南迁移可选择允许逆转此类操作(通过询问开发人员是否为恢复的字段提供默认值,或禁止反向迁移),这似乎不是所有新奇特的Django 1.7迁移的情况. .
问题:使用Django …
运行简单的.py或.pyw python文件会导致python.exe显示在任务管理器下.
python myApp.py
python myApp.pyw
Run Code Online (Sandbox Code Playgroud)
然而,当我们尝试不使用控制台运行,该脚本不会出现跑,也不python.exe或pythonw.exe在任务管理器中出现
pythonw myApp.pyw
pythonw myApp.py
Run Code Online (Sandbox Code Playgroud)
我们如何解决问题?系统运行的是Python 2.7.8 x64.
我正在关注Python GTK + 3教程,我正在尝试在virtualenv中运行正在运行的安装.我已经通过Ubuntu包管理器安装了python3-gi.事情看起来像这样:
:~$ mkvirtualenv py3 --python=/usr/bin/python3
Running virtualenv with interpreter /usr/bin/python3
Using base prefix '/usr'
New python executable in py3/bin/python3
Also creating executable in py3/bin/python
Installing setuptools, pip...python
done.
(py3):~$ python
Python 3.4.0 (default, Apr 11 2014, 13:05:11)
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import gi
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named 'gi'
>>>
(py3):~$ deactivate
:~$ …Run Code Online (Sandbox Code Playgroud) 我有以下型号:
class UserProfile(models.Model):
user = models.OneToOneField(User)
class Property(models.Model):
user = models.ForeignKey(User)
Run Code Online (Sandbox Code Playgroud)
我想创建一个TabularInline显示连接到UserProfile其Django管理页面上的特定属性的每个属性.这里的问题当然是Property没有ForeignKey直接的UserProfile,所以我不能简单地写
class PropertyTabularInline(admin.TabularInline):
model = Property
class UserProfileAdmin(admin.ModelAdmin):
inlines = (PropertyTabularInline,)
Run Code Online (Sandbox Code Playgroud)
我怎样才能轻松做到我想要的?
文档:https://docs.djangoproject.com/en/1.7/topics/auth/default/#django.contrib.auth.login
当您手动登录用户时,必须在调用login()之前调用authenticate().authenticate()在用户上设置一个属性,注意哪个身份验证后端成功验证了该用户(有关详细信息,请参阅后端文档),稍后在登录过程中需要此信息.如果您尝试直接登录从数据库中检索的用户对象,则会引发错误.
那么为什么authenticate和login2个独立的功能呢?据我所知,authenticate只需验证登录信息即可.login将获取用户对象并设置cookie.我认为它们是分开的唯一原因是因为也许你可以放入不同的用户对象,比如用户有2个帐户合并.也许您想先验证电子邮件地址.这就是为什么它们是单独的功能login而不包装authenticate?
python ×9
django ×8
django-1.7 ×2
buildout ×1
django-1.8 ×1
django-admin ×1
django-south ×1
python-2.7 ×1
python-3.x ×1
pythonw ×1
validation ×1
virtualenv ×1
windows-7 ×1