[Answer]-Creating a relation out of two ForeignKeys in the same model

1๐Ÿ‘

โœ…

If you want it in admin only then you can override the save_model() method:

class RoleAdmin(admin.ModelAdmin):

    def save_model(self, request, obj, form, change):
        obj.save()
        if not change:
            obj.member.band.add(obj.band)

Another option is to use the post_save signal. If this case the code will run outside the admin too:

def add_band(sender, instance, created, **kwargs):
    if created:
        instance.member.band.add(instance.band)

post_save.connect(add_band, sender=Role, dispatch_uid="add_band")
๐Ÿ‘คcatavaran

Leave a comment