[Fixed]-Content type dropdown not displayed django admin form

1πŸ‘

It seems that you configured your application wrong.

My project structure

app
|-settings/
|-forums/
|--models.py
|--admin.py
|-posts/
|--models.py
|--admin.py

So posts/models.py

class Post(models.Model):
    FEATURE_LIMITS = models.Q(app_label='forums', model='forum')

    name = models.CharField(max_length=100)

    content_type = models.ForeignKey(ContentType, limit_choices_to=FEATURE_LIMITS, on_delete=models.CASCADE)
    object_id = models.PositiveIntegerField()
    content_object = GenericForeignKey('content_type', 'object_id')

posts/admin.py

from .models import Post

class PostInline(GenericTabularInline):
    model = Post

forums/models.py

class Forum(models.Model):
    # other fields
    name = models.CharField(max_length=100)
    posts = GenericRelation('posts.Post')

forums/admin.py

from .models import Forum
from posts.admin import PostInline

@admin.register(Forum)
class ForumAdmin(admin.ModelAdmin):
    inlines = [
        PostInline,
    ]

And everything works like a charm.

I have a guess you have infinite loop because tried to register models in the wrong app.(model Forum in app posts)

πŸ‘€vishes_shell

0πŸ‘

I was using admin.widgets.ForeignKeyRawIdWidget widget which prevented foreign key fields to display normally. Since

content_type = models.ForeignKey(ContentType, limit_choices_to=FEATURE_LIMITS, on_delete=models.CASCADE)

content_type is a foreign key field, there was no dropdown.
To prevent this from happening, make following changes in admin.py:

class PostAdmin(admin.ModelAdmin):
    raw_id_field_excludes = 'content_type'

admin.site.register(Post, PostAdmin)
πŸ‘€rohanagarwal

Leave a comment