Hello, I have a model called Attr (attributes), its simple key,val lookup for my application’s form to use,
class Attr(models.Model):
name = models.CharField(max_length=20, db_index=True)
val = models.CharField(max_length=30)
class Meta:
constraints = [models.UniqueConstraint(fields=['name', 'val'], name='unique attr')]
question, how do I get my Admin panel to show “name” and “val” fields in the admin panel? (I registered this model with admin.py)
Attr.object(1)
Attr.object(2)
etc
also, how can I pass the “val” data to a template, for example I want my form to have a select field w values that come from this Attr table, ie
in my forms.py
eye_color = Attr.objects.filter(name=“eye_color”)
my template also shows Attr.object(1), etc
I think I figured out the Admin panel part, updated the Attr model to have def str method
class Attr(models.Model):
name = models.CharField(max_length=20, db_index=True)
val = models.CharField(max_length=30)
class Meta:
constraints = [models.UniqueConstraint(fields=['name', 'val'], name='unique attr')]
def __str__(self):
ret = self.name + ', ' + self.val
return ret
now admin form displays name , val
which is more user friendly than Attr.object(1) etc
There is another thing that I can suggest you can use it too.
In admin.py do the following code and see the changes in admin site .
from django.contrib import admin
from .models import Attr
@admin.register(Attr)
class AttrAdmin(admin.ModelAdmin):
list_display = ["name", "val"]
thanks this works, but had to use a tuple
@admin.register(Attr)
class AttrAdmin(admin.ModelAdmin):
list_display = ("name", "val")