ManyToOne Dropdown inline

I have a Django project for keeping track of computers and parts. I have several different models set up for various types of parts, and they have a foreign key pointing to a model for a built computer. As it stands I can only assign a computer to a part, but I would like to do the opposite.

Given something like the following:

class Computer(models.Model):

class PartType1(models.Model):
    name = models.CharField(max_length=200)
    installed_in = models.ForeignKey(Computer, on_delete=models.SET_NULL, null=True)

class PartType2(models.Model):
    name = models.CharField(max_length=200)
    installed_in = models.ForeignKey(Computer, on_delete=models.SET_NULL, null=True)

I created some inlines hoping that would work:

class PartType1Inline(admin.StackedInline):
    model = PartType1

class PartType2Inline(admin.StackedInline):
    model = PartType2

class ComputerAdmin(admin.ModelAdmin):
    inlines = (PartType1Inline, PartType2Inline, )

But this only allows me to create new parts from the Computer page. What I would really like is a simple dropdown allowing me choose existing objects from PartType1, PartType2 etc that have a null value for installed_in. How can I achieve this?

Side note: I think you may have a data-model issue. The way you have these models defined, each PartType1 may only be installed in one computer. (That may be what you want, but on the surface it doesn’t appear that way to me.)

Question: Do you only have PartType1 and PartType2? Or does the list extend farther than that?

It’s not clear what you’re looking to do here. You wrote:

I read that as saying that you want to assign a part to a computer.

If so, you would then go to the PartType1 model in the admin, and select the instance that you want to assign. It should show you a drop-down with the list of Computer.

This seems to identify a different situation. You’d want to create your own view for this - unless what you’re really asking for is to have the inlines only show you the instances of PartType1 that aren’t currently assigned to a Computer, in which case - yes - you can do that in the admin.

Sorry, I think in my effort to not overcomplicate the question I oversimplified it.

My models are actually named for various parts: CPU, RAM, Motherboard etc. Each record correlates to an actual, physical part. So only being able to have a part installed in one computer is what I want.

Currently to set a part as installed I need to visit the edit page for each part and set that value, I would prefer to do it all from the ‘Computer’ page, in order to save some time. So instead of creating a new instance (default behaviour for an inline) I just want to select an existing instance and update it to point to the Computer instance being created or edited.

For clarity, that’s not the default. The default is to allow you to edit those entries already related, and to add new entries.

I don’t think an inline is going to do that for you.

You might be able to achieve this using a custom form for the admin, with custom fields for the to-be-related classes. But this is really getting outside what the admin is designed or intended to do. You’re quickly approaching the point where you’re going to be better off building your own view for this.

Sounds like a lot more complexity than I expected for just a hobby project. I’ll stick with how it is now even if it’s slightly inconvenient and if I have a bunch of free time someday I’ll look into custom views.

Really appreciate the help!