小编Jef*_*_Hd的帖子

Django Admin Show Image来自Imagefield

虽然我可以在list_display中显示上传的图像,但是可以在每个模型页面上执行此操作(如更改模型所获得的页面)?

快速样本模型将是:

Class Model1(models.Model):
    image = models.ImageField(upload_to=directory)
Run Code Online (Sandbox Code Playgroud)

默认管理员显示上传图像的网址,但不显示图片本身.

谢谢!

django django-admin

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

TypeError(param`content'的预期Hash(获取字符串)

我目前正在尝试将widgEditor添加到ruby on rails上的表单中,但每当我点击提交时,我都会收到以下错误:

TypeError (expected Hash (got String) for param `content'
Run Code Online (Sandbox Code Playgroud)

表格代码如下:

<div class="field">
<%= f.label :content %><br />
<%= f.text_area :content,  :cols => "20", :rows=>"4", :class=>"widgEditor" %>
</div>
<div class="actions">
    <%= f.submit %>
 </div>
Run Code Online (Sandbox Code Playgroud)

有没有人有这个问题的经验?

javascript ruby-on-rails

7
推荐指数
2
解决办法
5604
查看次数

从 Admin Tabular Inline 访问外键字段

我正在尝试访问 Django 管理中表格内联中的外键字段。

尽管我尽了最大的努力,但我似乎无法让它发挥作用。我目前的代码是:

class RankingInline(admin.TabularInline):
    model = BestBuy.products.through
    fields = ('product', 'account_type', 'rank')
    readonly_fields = ('product', 'rank')
    ordering = ('rank',)
    extra = 0

    def account_type(self, obj):
        return obj.products.account_type
Run Code Online (Sandbox Code Playgroud)

结果是:

'RankingInline.fields' refers to field 'account_type' that is missing from the form.
Run Code Online (Sandbox Code Playgroud)

我也尝试过使用 model__field 方法,我将其用作:

fields = ('product', 'product__account_type', 'rank')
Run Code Online (Sandbox Code Playgroud)

结果是:

'RankingInline.fields' refers to field 'product__account_type' that is missing from the form.
Run Code Online (Sandbox Code Playgroud)

模型定义如下:

class Product(BaseModel):  
    account_type = models.CharField(choices=ACCOUNT_TYPE_OPTIONS, verbose_name='Account Type', max_length=1, default='P')

class Ranking(models.Model):
    product = models.ForeignKey(Product)
    bestbuy = models.ForeignKey(BestBuy)
    rank …
Run Code Online (Sandbox Code Playgroud)

django django-models django-admin

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

django.db.utils.IntegrityError:1452'无法添加或更新子行:外键约束失败

我正在尝试将提供者上的foreignKey和bestbuy_type上的M2M字段链接起来 - 但是每当我尝试将任何内容保存到这些字段中的任何一个时,我都会收到错误:

(1452, 'Cannot add or update a child row: a foreign key constraint fails (`savingschampion`.`products_masterproduct`, CONSTRAINT `provider_id_refs_id_2ea9c584` FOREIGN KEY (`provider_id`) REFERENCES `products_provider` (`id`))')
Run Code Online (Sandbox Code Playgroud)

这些字段在我的模型中指定为:

class MasterProduct(BaseModel):
    provider = models.ForeignKey('products.Provider', related_name = 'master_products', blank=True, null=True)
    bestbuy_type = models.ManyToManyField('products.BestBuy',blank=True, null=True)
    ...other (no relationship) fields which work fine
Run Code Online (Sandbox Code Playgroud)

使用它确实会在django admin中为字段填充正确的值,但是在保存时会产生错误.

使用MySQL和指定的引擎是:

'ENGINE': 'django.db.backends.mysql'
Run Code Online (Sandbox Code Playgroud)

有谁知道为什么会发生这种情况?

mysql django

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