[Fixed]-Django admin foreign key values custom form

1👍

What if you create a new model, say:

from django.dispatch import receiver


class PieceSelector(models.Model):
    piece = models.ForeignKey(Piece)

    def __unicode__(self):
        return piece.some_field

@receiver(post_save, sender=Piece)
def piece_post_save_signal_receiver(sender, **kwargs):
    if kwargs['created']:
        PieceSelector.objects.create(piece=kwargs['instance'])

Now, when you create a Piece model object, you should create PieceSelector object too. You can do it using post_save signal of the Piece model and it will provide all the pieces in a dropdown.

When in the admin.py, use PieceSelector as a StackedInline for Puzzle model.

Leave a comment