Answer the question
In order to leave comments, you need to log in
Django Admin Panel. How to display fields dynamically based on a variable in settings.py?
Hi {{ user.username }}.
Need your help/advice on implementing custom fields in Django admin.
Essence:
Based on certain values, add additional fields to the object (model) when it is created in the admin panel.
Input:
Variable in settings.py:
TARGET_LANGUAGES = (
'ru', 'Russian',
'es', 'Spanish',
)
class Topic(models.Model):
name = models.CharField(max_length=255)
category = models.ForeignKey(Category)
def __unicode__(self):
return self.name
class LocalizedTopic(models.Model):
topic = models.ForeignKey(Topic)
content = models.CharField(max_length=255)
lang = models.ForeignKey(Language, default='en')
def __unicode__(self):
return self.topic.name
Answer the question
In order to leave comments, you need to log in
Do you want to make LocalizedTopic inline for Topic? Their number can be limited by setting max_num = len(TARGET_LANGUAGES)
class LocalizedTopicAdmin(admin.StackedInline):
model = LocalizedTopic
max_num = len(settings.TARGET_LANGUAGES)
class TopicAdmin(admin.ModelAdmin):
inlines = [LocalizedTopicAdmin]
from django.conf import settings
for lang in settings.TARGET_LANGUAGES:
# Add custom field...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question