I have django models like this:
models.py
#BaseClass
class FarmBaseModel(models.Model):
created_at = models.DateTimeField(
auto_now_add=True,
editable=False,
)
updated_at = models.DateTimeField(auto_now_add=True, blank=True)
farm_id = models.IntegerField()
class Meta:
abstract = True
#RackModel
class Rack(FarmBaseModel):
name = models.CharField(max_length=255)
def __str__(self):
return f"{self.name}"
#LevelModel
class Level(FarmBaseModel):
name = models.CharField(max_length=255)
rack_id = models.ForeignKey(Rack, on_delete=models.CASCADE)
def __str__(self):
return f"{self.rack_id}-{self.name}"
admin.py
class LevelInline(admin.TabularInline):
model = Level
extra = 1
class RackAdmin(admin.ModelAdmin):
list_display = ("name", "farm_id", "created_at", "updated_at")
search_fields = ["name", "farm_id"]
inlines = [LevelInline, ]
This creates an Admin Page to add levels to a Rack which looks like this:
I would like to have the inline form (LevelInline) just display the name field and use the same value for farm_id that is defined on the Rack form.
I need to know how to override the Admin models to handle this.
FYI: I know the correct way here is to remove the farm_id fild from Level Model and use the farm_id from Rack Model but still i need to keep the farm-id field on both the models.
I have tried overriding the _get_formset method on the inline admin but i cannot figure out how to use the value farm_id from RackAdmin to populate this.