Related lookup field foreign key doesn't work in inline Django

跟風遠走 提交于 2019-12-11 08:36:07

问题


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.


回答1:


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',)



回答2:


I have the same situation, but my raw_id_fields are in admin.TabularInline, the problem is with magnifying glass button, not showing, but the input is changed:

# admin
class ApartmentInline(admin.TabularInline):
    model = Apartment
    max_num = 12
    extra = 1
    raw_id_fields = ('propaganda',)

class BuildingAdmin(admin.ModelAdmin):
    inlines = [ApartmentInline]

# models
class Apartment(models.Model):
    status = models.ForeignKey(Status)
    building = models.ForeignKey(Building)
    propaganda = models.ForeignKey(Propaganda)

Thanks!

Edit:

Solved! Need a object in database.



来源:https://stackoverflow.com/questions/11018022/related-lookup-field-foreign-key-doesnt-work-in-inline-django

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!