django-chained-selectbox icon indicating copy to clipboard operation
django-chained-selectbox copied to clipboard

When parent_field belongs to a GenericForeignKey

Open evildmp opened this issue 10 years ago • 1 comments

This is a neat little tool. However, I've encountered an issue.

The model:

class MyModel(models.Model):
    destination_content_type = models.ForeignKey(ContentType )
    destination_object_id = models.PositiveIntegerField()
    destination_content_object = generic.GenericForeignKey(
        'destination_content_type', 'destination_object_id'
        )

    # this is the field that needs to to have dynamic choices (FORMATS) based on 
    # the selected destination_content_type 
    format = models.CharField(
        "Item format", max_length=25, choices=FORMATS,
        default="title"
        )

In the admin form:

format = ChainedChoiceField(
     parent_field='destination_content_type', 
    ajax_url='/chainedselectchoices'
)

Unfortunately this behaves differently for new and existing (already-saved) items.

New items return an int as the parent_value in the view (useful, because you can get to the class from that). Existing items return a unicode representation of the model name (not useful!).

Any suggestion on how to deal with this?

evildmp avatar Apr 15 '14 11:04 evildmp

Actually I think this affects all ForeignKeys.

evildmp avatar Apr 15 '14 14:04 evildmp