Bug? how to load only the related field values of a model

hi guys!

not sure if im getting a bug or what but its driving my nuts lol

So, im trying to create an inlineformset_factory form that allows me to create a supplier, a list of colors of tables (that theses providers sell), and the table dimensions of those suppliers.

The thing is that the “color” field in the add/change admin form always shows me all the colors for all the suppliers instead of only those of that supplier . Im i missing something like creating a custom set for that field? is this possible? is this a bug?

Not sure how to face this issue, what could i do?

Thank you!
(sorry for my bad eng)

django.VERSION
(5, 0, 0, ‘final’, 0)

Screenshot:

# models.py
from django.db import models 
from django.contrib.auth.models import User

class SupplierStock(models.Model):
    name = models.CharField(max_length=100)
    telefono = models.CharField(max_length=100,blank=True,null=True)
    celular = models.CharField(max_length=100,blank=True,null=True)
    paginaweb = models.CharField(max_length=300,blank=True,null=True)
    
    def __str__(self):
        return self.name

class SupplierColorStock(models.Model):
    name = models.CharField(max_length=100)
    supplier = models.ForeignKey(SupplierStock, on_delete=models.PROTECT)

    def __str__(self):
        return self.name
    
    class Meta:
        verbose_name = "Color"
        verbose_name_plural = "Colors of Supplier"

class SupplierTablaStock(models.Model):
    supplier = models.ForeignKey(SupplierStock, on_delete=models.PROTECT) 
    color = models.ForeignKey(SupplierColorStock, on_delete=models.PROTECT)
    width = models.PositiveIntegerField(blank=False , null=False)
    height = models.PositiveIntegerField(blank=False , null=False) 
    thickness = models.PositiveIntegerField(blank=False , null=False)  

    def __str__(self):
        return f"Table {self.width}cm x {self.height}cm, Color: {self.color}, Supplier: {self.Supplier}"
    
    class Meta:
        verbose_name = "Tabla"
        verbose_name_plural = "Tables of Supplier"

# admin.py 
from django.contrib import admin
from django.forms import inlineformset_factory
from .models import SupplierStock, SupplierColorStock, SupplierTablaStock
from .forms import SupplierTablaStockForm

SupplierTablaStockInlineFormSet = inlineformset_factory(
    SupplierColorStock,
    SupplierTablaStock,
    fields=('width', 'height', 'thickness', 'color'),
    extra=1,
    form=SupplierTablaStockForm,
)

class SupplierTablaStockInline(admin.TabularInline):
    model = SupplierTablaStock
    extra = 1
    formset = SupplierTablaStockInlineFormSet 

class SupplierColorStockInline(admin.TabularInline):
    model = SupplierColorStock
    extra = 1 
    template = "admin/muebles/stock/suppliercolorstock_inline.html" 

class SupplierStockAdmin(admin.ModelAdmin):
    inlines = [SupplierColorStockInline, SupplierTablaStockInline]
    class Media:
        css = { 'all': ('/static/muebles/css/admin.css',), }

admin.site.register(SupplierStock, SupplierStockAdmin)
# forms.py
from django import forms
from .models import SupplierTablaStock 

class SupplierTablaStockForm(forms.ModelForm):
    class Meta:
        model = SupplierTablaStock
        fields = ('width', 'height', 'thickness', 'color')

Side Note: When posting code or traceback messages here, surround the code (or traceback) between lines of three backtick - ` characters. This means you’ll have a line of ```, then your code, then another line of ```. This forces the forum software to keep the text properly formatted.
(I’ve taken the liberty of fixing your original post for this.)

1 Like