相关查找字段外键在内联Django中不起作用 [英] Related lookup field foreign key doesn't work in inline Django

查看:200
本文介绍了相关查找字段外键在内联Django中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的表格线字段有问题。我有这样的模型

  class Product(models.Model):
....

class Pemesanan(models.Model):
produks = models.ManyToManyField(Product,verbose_name = uKode Produk,through ='Foo')

class Foo(models。模型):
product = models.ForeignKey(Product)
...

Foo类是Pemesanan类和Class Product类的中介类(manytomany字段)。它拥有Pemesanan类的外键字段。 Foo类在change_form模板中以表格形式显示,如 http:// upload。 ui.ac.id/?a=d&i=845380



但是我的问题是该字段产品不会显示为相关的查找字段它显示为普通的形式(不是内联的)。这是我的管理员

  class FooInline(admin.TabularInline):
model = Foo
extra = 0
allow_add = True

class PemesananAdmin(admin.ModelAdmin):
....
search_fields = ['produks']
raw_id_fields =('
related_lookup_fields = {
'm2m':['produks'],
}
inlines = [
FooInline,
]
exclude =('produks',)

我使用自动填充,但是看起来很难因为这个教程是不完整的。那么有没有办法让我在表格线上找到相关的查询?非常感谢:D。

解决方案

所以,是的,我想我只是误会你在问什么。您只是想要相关的查找弹出窗口在每个内联中选择一个产品,而不是一个选择框。你已经知道了 raw_id_fields ;问题是你需要指定在内联模型的管理员,而不是主要的父模型管理员。

  class FooInline(admin .TabularInline):
model = Foo
extra = 0
allow_add = True
raw_id_fields =('product',)
pre>

i've a problem with my tabularinline field. I have model like this

class Product(models.Model):
....

class Pemesanan(models.Model):
produks = models.ManyToManyField(Product, verbose_name=u"Kode Produk", through='Foo')

class Foo(models.Model):
product = models.ForeignKey(Product)
...

Class Foo is an intermediary class (manytomany field) with class Pemesanan and Class Product. It has a foreign key field to Class Pemesanan. Class Foo is displayed as an tabularinline in change_form template like this http://upload.ui.ac.id/?a=d&i=845380

But my problem is that field product doesn't show as a related lookup field as it is shown up as an ordinary form (not in inline). This is my Admin

class FooInline(admin.TabularInline):
model = Foo
extra = 0
allow_add = True

class PemesananAdmin(admin.ModelAdmin):
....
search_fields = ['produks']
raw_id_fields = ('produks',)
related_lookup_fields = {
'm2m': ['produks'],
}
inlines = [
FooInline,
]
exclude = ('produks',)

I have use autocomplete, but it seems so hard to implement here because the tutorial is incomplete. So is there a way for me to get my related lookup works in my tabularinline ? Thank you very much :D.

解决方案

So, yeah, I think I did simply misunderstand what you were asking. You're just wanting the related lookup popup to select a product in each inline, rather than a select box. You already know about raw_id_fields; the problem is that you need to specify that on the inline model admin, not the main parent model admin.

class FooInline(admin.TabularInline):
    model = Foo
    extra = 0
    allow_add = True
    raw_id_fields = ('product',)

这篇关于相关查找字段外键在内联Django中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆