I have a library with shelves and books. I point each book to one shelf in a one-to-many relationship. If a book is pointing to a Null it means that it's in the library, but not in the shelf yet.
#models.py
class Shelf(models.Model):
    pass
class Book(models.Model):
    shelf = models.ForeignKey(Shelf, blank=True, null=True)
Then:
#admin.py
class BookInLine(admin.TabularInLine):
    model = Book
    extra = 0
class Shelf(admin.ModelAdmin):
    inlines = [ BookInLine, ]
When I edit the Shelf I can see and modify all the books that are in that shelf.
Problem:
Null).Hi the following code worked for me:
from widgets import ImproveRawIdFieldsForm
class BookInline(admin.TabularInline):
    model = Book
    raw_id_fields=('shelf',)
    extra =1
class Shelf(ImproveRawIdFieldsForm):
    inlines = [BookInline,]
It creates an admin view where you will se the normal Shelf stuff and the additional inline which is a raw id field and you have the posssibility to add new relations and you can chose from existing objects with the "magnifier" icon, which results in a pop-up of a list of all existing books. Besides chose one Book in the pop-up you can also create new Books there. So from my understanding this solves all your requirements
a better solution for this problem is explained here: one-to-many inline select with django admin
edited for your use-case:
#models.py
class Book(models.Model):
    shelf = models.ForeignKey(Shelf, blank=True, null=True, related_name="in_shelf")
#admin.py
class ShelfForm(forms.ModelForm):
    class Meta:
        model = Shelf
    books = forms.ModelMultipleChoiceField(queryset=Book.objects.all())
    def __init__(self, *args, **kwargs):
        super(ShelfForm, self).__init__(*args, **kwargs)
        if self.instance:
            if self.instance.in_shelf:
                self.fields['books'].initial = self.instance.in_shelf.all()
            else:
                self.fields['books'].initial = []
    def save(self, *args, **kwargs):    
        instance = super(ShelfForm, self).save(commit=False)
        self.fields['books'].initial.update(shelf=None)
        self.cleaned_data['books'].update(shelf=instance)
        return instance
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With