sergzach
sergzach

Reputation: 6754

Admin: Displaying IntegerField with not predefined choices as select with options

I have a model with GenericForeignKey:

class Breadcrumbs( models.Model ):

    title = models.CharField( max_length = 256 )

    limit = models.Q( app_label = 'news', model = 'NewsPost' )

    content_type = models.ForeignKey( ContentType, limit_choices_to = limit )
    object_id = models.PositiveIntegerField()
    content_object = generic.GenericForeignKey( 'content_type', 'object_id' )

    class Meta:
        db_table = 'breadcrumbs'

By default there is an input text widget for object_id:

enter image description here

How to make it as select with options? Also I would like to fill it with NewsPost ids (or with unicode() of NewsPost) by default.

Upvotes: 0

Views: 85

Answers (1)

Thomas Orozco
Thomas Orozco

Reputation: 55207

You shouldn't use a PositiveIntegerField here. You should use a ForeignKey to NewsPost.

This will do the two things you need with no additional effort.

Upvotes: 2

Related Questions