试着这样做:
更新:
wishList = WishList.objects.get(pk=20)
matches = [val for val in Store.attribute_answers.all() if val in wishList.attribute_answers]
Run Code Online (Sandbox Code Playgroud)
得到这个......
'ManyRelatedManager' object is not iterable
Run Code Online (Sandbox Code Playgroud)
这两个领域都很多,所以怎么做呢?
在保存模型"产品"后,我希望上传的图像与pk的名称相同,例如22.png或34.gif我不想仅仅更改名称的图像格式.如何才能做到这一点?我的模型的例子到目前为止......
image = models.ImageField(
upload_to="profiles",
height_field="image_height",
width_field="image_width",
null=True,
blank=True,
editable=True,
help_text="Profile Picture",
verbose_name="Profile Picture"
)
image_height = models.PositiveIntegerField(null=True, blank=True, editable=False, default="100")
image_width = models.PositiveIntegerField(null=True, blank=True, editable=False, default="100")
Run Code Online (Sandbox Code Playgroud) 我有以下使用Django REST Framework的 Serializer .
这就是我到目前为止......
serializer.py
class ProductSerializer(serializers.ModelSerializer):
score = serializers.SerializerMethodField('get_this_score')
class Meta:
model = Product
fields = ('id', 'title', 'active', 'score')
def get_this_score(self, obj):
profile = Profile.objects.get(pk=19)
score = [val for val in obj.attribute_answers.all() if val in profile.attribute_answers.all()]
return (len(score))
Run Code Online (Sandbox Code Playgroud)
urls.py
url(r'^products/(?P<profile_id>.+)/$', ProductListScore.as_view(), name='product-list-score'),
Run Code Online (Sandbox Code Playgroud)
这段代码存在一些问题.
1)pram pk = 19是硬编码的应该是self.kwargs['profile_id'].我尝试过但尝试过,但我不知道如何将kwarg传递给方法并且无法使profile_id工作.即我无法从网址获取.
2)这些代码中是否有任何代码?我已经尝试添加模型,但再次可以通过args.
models.py 即方法类
def get_score(self, profile):
score = [val for val in self.attribute_answers.all() if val in
profile.attribute_answers.all()]
return len(score)
Run Code Online (Sandbox Code Playgroud) 我的Django(Grappelli主题)管理员列出了我的所有应用程序,然后模型名称以复数形式显示.要将名称转换为复数,Django会在模型名称中添加"s",但其中一些是不正确的.例如,"产品类别"变为"产品类别"
如何更改管理视图中显示的名称?
使用Django休息
下面是我如何使用serializer.py.
class ProfileSerializer(serializers.ModelSerializer):
class Meta:
model = Profile
fields = ('id', 'name', 'active', 'type')
Run Code Online (Sandbox Code Playgroud)
类型是平面视图
然后我改变它,所以'type'嵌套在每个配置文件中,就像这样......
class TypeSerializer(serializers.ModelSerializer):
class Meta:
model = Type
fields = ('id', 'name', 'active')
class ProfileSerializer(serializers.ModelSerializer):
type = TypeSerializer()
class Meta:
model = Profile
fields = ('id', 'name', 'active', 'type'')
Run Code Online (Sandbox Code Playgroud)
现在这种方法很完美,但我现在只能在配置文件详细信息中更新"类型",现在它是只读的.
如何在创建新配置文件时添加类型并仍保留此嵌套视图?
我希望我已经清楚地解释了这一点.
更新:
好的,我刚看过这个:
注意:嵌套序列化程序仅适用于只读表示形式,因为如果在更新实例时使用它们,则会出现模糊或不明显的行为.对于读写表示,您应始终使用FlatField子类之一来使用平面表示.
这是有道理的.所以我改成了....
type = serializers.PrimaryKeyRelatedField()
Run Code Online (Sandbox Code Playgroud)
这使它回到POST并工作,但这是一个耻辱,我不能用ID和名称代表'类型',这样对最终用户更有意义吗?
使用Django REST 框架,我在下面有以下序列化程序。我想将(嵌套的)相关对象(ProductCatSerializer)添加到 ProductSerializer。我尝试了以下....
class ProductCatSerializer(serializers.ModelSerializer):
class Meta:
model = ProductCat
fields = ('id', 'title')
class ProductSerializer(serializers.ModelSerializer):
"""
Serializing the Product instances into representations.
"""
ProductCat = ProductCatSerializer()
class Meta:
model = Product
fields = ('id', 'title', 'description', 'price',)
Run Code Online (Sandbox Code Playgroud)
所以我想要发生的是 Products 显示其嵌套在结果中的相关类别。
谢谢你。
更新:
使用 depth = 2 选项(感谢 Nandeep Mali )我现在得到嵌套值,但它们只显示使用 ID 而不是 keyparis 像其余的 json 请求(见下面的类别)。它几乎是正确的。
"results": [
{
"id": 1,
"title": "test ",
"description": "test",
"price": "2.99",
"product_url": "222",
"product_ref": "222",
"active": true, …Run Code Online (Sandbox Code Playgroud) 我的models.py中有一个图像字段...
qr_image = models.ImageField(
upload_to="public/uploads/",
height_field="qr_image_height",
width_field="qr_image_width",
null=True,
blank=True,
editable=False
)
Run Code Online (Sandbox Code Playgroud)
当editable=False我尝试显示它时,我遇到了一个令人讨厌的错误.我不想领域可编辑的,但是我要在图像中管理员显示" 编辑页面即字段集
我是Django的新手,有人可以告诉我这是否可行,并指出我在这里正确的方向?
谢谢.
我正在使用Django Rest Framework我注意到在Web上可浏览的API部分有一个名为'options'的按钮,当点击它时会显示以下内容...
HTTP 200 OK Vary: Accept Content-Type: text/html Allow: HEAD, GET, OPTIONS
{
"parses": [
"application/json",
"application/x-www-form-urlencoded",
"multipart/form-data"
],
"renders": [
"application/json",
"text/html"
],
"name": "Products",
"description": "API endpoint."
}
Run Code Online (Sandbox Code Playgroud)
我的问题是,无论如何我可以在这里列出所有过滤器选项这个网址的其他东西?
我的models.py中有以下图像字段(参见下面的代码)
我想设置一个固定的图像宽度和高度,所以它总是100x100px
下面的代码是系统中已有的代码,我不确定如何传递宽度和高度,或者是否可以使用此代码将宽度和高度设置为固定大小.
image = models.ImageField(
upload_to="profiles",
height_field="image_height",
width_field="image_width",
null=True,
blank=True,
editable=True,
help_text="Profile Picture",
verbose_name="Profile Picture"
)
image_height = models.PositiveIntegerField(null=True, blank=True, editable=False, default="100")
image_width = models.PositiveIntegerField(null=True, blank=True, editable=False, default="100")
Run Code Online (Sandbox Code Playgroud) 我正在遵循本指南,但在保存时出现以下错误...
RuntimeError at /admin/products/product/2/
maximum recursion depth exceeded
Run Code Online (Sandbox Code Playgroud)
为什么我会收到这个错误?我在下面包含了完整的模型。
谢谢
模型.py:
class Product(models.Model):
title = models.CharField(max_length=60)
qr_url = models.URLField(blank=True)
qr_image = models.ImageField(
upload_to="public/uploads/",
height_field="qr_image_height",
width_field="qr_image_width",
null=True,
blank=True,
editable=False
)
qr_image_height = models.PositiveIntegerField(null=True, blank=True, editable=False)
qr_image_width = models.PositiveIntegerField(null=True, blank=True, editable=False)
#FK
category = models.ManyToManyField(ProductCategory)
attribute_answers = models.ManyToManyField(AttributeAnswers)
# Custom Managers
def __unicode__(self):
return self.title
def qr_code(self):
return '' % self.qr_image.url
qr_code.allow_tags = True
def product_pre_save(sender, instance, **kwargs):
if not instance.pk:
instance._QRCODE = True
else:
if hasattr(instance, '_QRCODE'):
instance._QRCODE = …Run Code Online (Sandbox Code Playgroud)